java - How to set TimeOut while HTTP Connection in Android? -
java - How to set TimeOut while HTTP Connection in Android? -
hello have code androidhive:
it works want install timeout - because if there low connection loading minutes app crashing @ , (asynctask). question: how can install timeout next code? let's shall timeout after 10 seconds?
thank u
public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request seek { // 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(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } seek { 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(); } grab (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // seek parse string json object seek { jobj = new jsonobject(json); } grab (jsonexception e) { log.e("json parser", "error parsing info " + e.tostring()); } // homecoming json string homecoming jobj; }
java android http
Comments
Post a Comment