i working on android application , getting data php webservice. before calling php webservice, checking internet connection. if connection there asynchronous task start. here getting issue i.e after checking internet connection , while executing background task in asynchronous method power going , @ time crash coming. took boolean variable , kept server=false if try action not executed. when there no internet connection when posting paramaeters php not going catch method , not displaying toast message "try again". please tell me went wrong.
my code: boolean server = true; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.test); if (havenetworkconnection()) { longoperation1 op = new longoperation1(); op.execute(""); } else { toast.maketext(getapplicationcontext(), "no network connection", toast.length_long).show(); } } private void loadfeed() { arraylist<namevaluepair> postparameters = new arraylist<namevaluepair>(); postparameters.add(new basicnamevaluepair("val1",value1)); postparameters.add(new basicnamevaluepair("val2",value2)); try { response = customhttpclient .executehttppost( "http://....................../file.php", postparameters); system.out.println("the ultimate response" + response); } catch (exception e) { server = false; e.printstacktrace(); } } private boolean havenetworkconnection() { boolean haveconnectedwifi = false; boolean haveconnectedmobile = false; connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo[] netinfo = cm.getallnetworkinfo(); (networkinfo ni : netinfo) { if (ni.gettypename().equalsignorecase("wifi")) if (ni.isconnected()) haveconnectedwifi = true; if (ni.gettypename().equalsignorecase("mobile")) if (ni.isconnected()) haveconnectedmobile = true; } return haveconnectedwifi || haveconnectedmobile; } private class longoperation1 extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { loadfeed(); return "executed"; } @override protected void onpostexecute(string result) { dialog1.dismiss(); try { if (server) { toast.maketext(getapplicationcontext(), "success", toast.length_long).show(); } else { toast.maketext(getapplicationcontext(), "try again", toast.length_long).show(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onpreexecute() { dialog1 = progressdialog.show(reuse.this, "please wait...", "retrieving data ...", true); } @override protected void onprogressupdate(void... values) { } }
Comments
Post a Comment