Save data to server data base in Android give unexpected error and close -
i make application track geo location , save data server mysql database. when run application show unexpected error , close application. times geolocation not pointing. when comment save code geo location tracking working nicely. cant find exact problem in here code
///////////////// updated locations///////////// public void onlocationchanged(location location) { string msg = "updated location: " + double.tostring(location.getlatitude()) + "," + double.tostring(location.getlongitude()); toast.maketext(this, msg, toast.length_short).show(); loadsave(); // report ui location updated } public void loadsave() { string mydataurl = "http://myrul.com/savedata.php"; //string type = params[0]; try { string longi = "aaa"; // double.tostring(location.getlongitude()); string lati = "aare";//double.tostring(location.getlatitude()); string user = "a"; string code = "b"; string date = "c"; url url = new url(mydataurl); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); httpurlconnection.setdoinput(true); outputstream outputstream = httpurlconnection.getoutputstream(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8")); string post_data = urlencoder.encode("longi", "utf-8") + "=" + urlencoder.encode(longi, "utf-8") + "&" + urlencoder.encode("lati", "utf-8") + "=" + urlencoder.encode(lati, "utf-8") + "&" + urlencoder.encode("user", "utf-8") + "=" + urlencoder.encode(user, "utf-8") + "&" + urlencoder.encode("code", "utf-8") + "=" + urlencoder.encode(code, "utf-8") + "&" + urlencoder.encode("date", "utf-8") + "=" + urlencoder.encode(date, "utf-8"); bufferedwriter.write(mydataurl); bufferedwriter.flush(); bufferedwriter.close(); outputstream.close(); } catch (malformedinputexception e) { e.printstacktrace(); } catch (ioexception ex) { ex.printstacktrace(); } }
stack trace
android thought exception because executed network task on main thread. have execute task on other thread put asynctask.
new asynctask<void, void, void>() { @override protected void doinbackground(final void... params) { loadsave(); return null; } }.execute();
Comments
Post a Comment