java - AsyncTask doInBackground comes with big delay -
can explain why asynctask comes big delay , in 1 case, , when have wifi network there no internet connectivity on network.
what reason that, , there way make bigger priority on async task?
i have had problem before, solved adding onpreexecute in order check if there internet connection before attempting execute doinbackground, check out code below:
public class httpgetproduct extends asynctask<void,void,string> { string result, rs; jsonarray jarray; string itemcode="",price,desc_ar,desc_en; @override protected void onpreexecute() { super.onpreexecute(); isonline=ping(); // use ping if connecting local ip address isonline= hasinternetconnection();// use if connecting internet } @override protected string doinbackground(void... params) { if(isonline) // executes if online try { string link = "http://"+ip+"/pricecheckerws.asmx/get_product_data?barcode="+barcode; url url = new url(link); httpclient client = new defaulthttpclient(); httpget request = new httpget(); request.seturi(new uri(link)); httpresponse response = client.execute(request); bufferedreader reader = new bufferedreader(new inputstreamreader(response.getentity().getcontent(), "utf-8"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } result = sb.tostring(); // somthing result if need rs = "sucessful"; } catch (exception e) { rs = "fail"; } return rs; } @override protected void onpostexecute(string s) { super.onpostexecute(s); } } public boolean ping() { string[] separated = ip.split(":"); string hostip=separated[0]; runtime runtime = runtime.getruntime(); try { process ipprocess = runtime.exec("/system/bin/ping -c 1 " + hostip); int exitvalue = ipprocess.waitfor(); return (exitvalue == 0); } catch (ioexception e) { e.printstacktrace(); } catch (interruptedexception e) { e.printstacktrace(); } return false; } public boolean hasinternetconnection() { try { connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); if (cm != null) { networkinfo = cm.getactivenetworkinfo(); if (i != null) { if (!i.isconnected()) return false; if (!i.isavailable()) return false; } if (i == null) return false; } else return false; } catch (exception e){ return false; } internet=true; return true; }
Comments
Post a Comment