android - Application Crashing frequently on httprequest -
while fetching data server error frequently. past few weeks happening. sometime works , not. logcat shows error in jsonparser file while
httpclient.execute
but unable rectify why?
exercise.java
list<namevaluepair> parametres = new arraylist<namevaluepair>(); // getting json string url parametres.add(new basicnamevaluepair("inputchar", inputchar)); **jsonobject json = jsonparser.makehttprequest(url_display_user, "get", parametres);** try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of products exercises = json.getjsonarray(tag_response); // looping through response (int = 0; < exercises.length(); i++) { jsonobject jsonobj = exercises.getjsonobject(i); // storing each json item in variable string id = jsonobj.getstring(tag_id); string activityname = jsonobj.getstring(tag_activity_name); string metvalue = jsonobj.getstring(tag_metvalue); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_id, id); map.put(tag_activity_name, activityname); map.put(tag_metvalue, metvalue); // adding hashlist arraylist exerciseitems.add(map); } return json.getstring(tag_message); } else { return json.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null;
jsonparser.java
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); system.out.println(is + "post"); }else if(method == "get"){ system.out.println("1"); // request method defaulthttpclient httpclient = new defaulthttpclient(); system.out.println("2"); string paramstring = urlencodedutils.format(params, "utf-8"); system.out.println("3"); url += "?" + paramstring; system.out.println("4"); httpget httpget = new httpget(url); system.out.println("5"); httpresponse httpresponse = httpclient.execute(httpget); system.out.println("6"); httpentity httpentity = httpresponse.getentity(); system.out.println("7"); = httpentity.getcontent(); system.out.println(is + "get"); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); log.i("tagcoverts", "["+json+"]"); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
i error while fetching data
one more error
you can go httpurlconnection ssl enabled , far more secure httpclient.
public class httppostdata extends asynctask<string, void, string>{ private string url; private jsonobject jsonobjsend; int mresponsecode = -1; private object totaltime; private long starttime; public httppostdata(string url, jsonobject jsonobjsend) { this.url = url; this.jsonobjsend = jsonobjsend; } @override protected string doinbackground(string... params) { string finalresult = null; url url = null; httpsurlconnection urlconnection = null; try { url=new url(url); urlconnection = (httpsurlconnection) url.openconnection(); urlconnection.setdooutput(true); urlconnection.setdoinput(true); urlconnection.setrequestmethod("post"); urlconnection.setusecaches(false); urlconnection.setconnecttimeout(500); urlconnection.setreadtimeout(500); urlconnection.setrequestproperty("content-type","application/json"); urlconnection.connect(); outputstreamwriter out = new outputstreamwriter(urlconnection.getoutputstream()); out.write(jsonobjsend.tostring()); out.close(); starttime = system.currenttimemillis(); try { mresponsecode = urlconnection.getresponsecode(); totaltime = system.currenttimemillis() - starttime; log.e("http post response code:", "" + mresponsecode); } catch (sslpeerunverifiedexception e) { log.e("exception", log.getstacktracestring(e)); } catch (ioexception e) { log.e("exception", log.getstacktracestring(e)); log.e("http post response code(2):", "" + mresponsecode); }
Comments
Post a Comment