Android App Crashes if no internet during server connection -
i calling url fetch data , insert in database. have provided no internet check. if open app without internet connection, works fins, pop comes.. if connect url when have internet , internet goes in middle process, app crashes, how fix it?
my code:
public class dopostpen extends asynctask<string, void, boolean> implements oncancellistener { @suppresswarnings("deprecation") @override protected boolean doinbackground(string... arg0) { try { httpclient client = new defaulthttpclient(); httpget request = new httpget("http://testapi.pharmeazy.in/api/medieazy/getallinvoices"); request.addheader("authorization", " basic ndlyawnva2pvawqwm2ptzglrawres09qzgzpamrmnzy0dda4nwp6mzcyohdzmkpjs1m4mta0c2nvctj1otrkazphd0vemzi3mka4wwfzzeu3mji3iubeiypvsfmq"); request.setheader(http.content_type, "application/json"); system.out.println("priinting"); httpresponse response = null; try { response = client.execute(request); log.d("response of request", response.tostring()); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } inputstream inputstream = response.getentity().getcontent(); string result = utils.convertinputstreamtostring(inputstream); system.out.println("server response :" + result + "\n" + inputstream); try { ja=new jsonarray(result); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (exception e) { log.e("clientserverdemo", "error:", e); toasttext2 = "connection error !"; } return true; } protected void onpreexecute() { //display progress dialog mprogresshud2 = progresshud.show(med.this,"getting rejected orders", true,false,this); } @override protected void onpostexecute(boolean valid) { mprogresshud2.dismiss(); // if server response successful for(int i=0;i<ja.length();i++){ try { jo=ja.getjsonobject(i); db.insertlabel(jo.get("id").tostring(), jo.get("customerid").tostring(), jo.get("pharmacyid").tostring(), jo.get("deliveryaddress").tostring(), jo.get("deliveryname").tostring(), jo.get("orderstatus").tostring(),jo.get("prescriptionattached").tostring(),jo.get("discount").tostring()); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } new dopost3().execute(); }
please help
why don't go httpurlconection
@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(5000); urlconnection.setreadtimeout(5000); 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); } }
check before calling asynctask
if(util.havenetworkconnection(this)) { httppostdata req=new httppostdata(url, createjsonobject(),this) ; req.execute(); }
Comments
Post a Comment