android - Json Parser Exception unable to parse -
this code printing me json parser exception.
in code want send latitudes server, keep on displaying me 2 errors:
- json exception unable parse
- async task #1 error
could please suggest me doing wrong?
here relevant code:
package com.example.gpstracking; public class androidgpstrackingactivity extends activity { button btnshowlocation; string lat = ""; string lng = ""; // gpstracker class gpstracker gps; private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); // jsonparser jsonparser = new jsonparser(); private static string url_create_product = "http://dyandroidapps.netii.net/" + "android_db/db_functncreate1.php"; // json node names private static final string tag_success = "success"; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnshowlocation = (button) findviewbyid(r.id.btnshowlocation); // show location button click event btnshowlocation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // create class object gps = new gpstracker(androidgpstrackingactivity.this); // check if gps enabled if (gps.cangetlocation()) { double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); lat = double.tostring(latitude); lng = double.tostring(longitude); toast.maketext( getapplicationcontext(), "your location " + "- \nlat: " + latitude + "\nlong: " + longitude, toast.length_long) .show(); new createnewproduct().execute(); // postdata(lat, lng); } else { // can't location // gps or network not enabled // ask user enable gps/network in settings gps.showsettingsalert(); } } }); } /* * public void postdata(string lat, string lng) { // create new httpclient * , post header httpclient httpclient = new defaulthttpclient(); * * httppost httppost = new * httppost("http://dyandroidappa.netii.net/android_db/create_product.php"); * * try { * * // execute http post request // httpresponse response = * httpclient.execute(htget); httpresponse response = * httpclient.execute(httppost); // string resp = * response.getstatusline().tostrinkatyg(); try // // * toast.maketext(this, resp, 5000).show(); * * } catch (clientprotocolexception e) { toast.maketext(this, "error", * 5000).show(); } catch (ioexception e) { toast.maketext(this, "error", * 5000).show(); } } */ class createnewproduct extends asynctask<string, string, string> { // * before starting background thread show progress dialog @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(androidgpstrackingactivity.this); pdialog.setmessage("sending markers.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } // * creating product protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("lat", lat)); params.add(new basicnamevaluepair("lng", lng)); // params.add(new basicnamevaluepair("description", description)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest(url_create_product, "post", params); // check log cat response log.d("create response", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { // created product toast.maketext(getapplicationcontext(), "data posted", toast.length_short).show(); // closing screen finish(); } else { // failed create product toast.maketext(getapplicationcontext(), "data not posted", toast.length_short).show(); } } catch (jsonexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "json exception", toast.length_short).show(); } catch (exception e) { toast.maketext(getapplicationcontext(), "only exception", toast.length_short).show(); } return null; } // after completing background task dismiss progress dialog protected void onpostexecute(string file_url) { // dismiss dialog once done pdialog.cancel(); } } }
and jsonparser class is
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if (method == "post") { // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); //httppost httppost = new httppost(url); // httppost.setheader("content-type","application/json"); httppost.setheader("content-type","application/json"); 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); httpget.setheader("content-type","application/json"); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { 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(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
and logcat is
06-27 18:13:28.160: e/json parser(2583): error parsing data org.json.jsonexception: end of input @ character 0 of 06-27 18:13:28.170: w/dalvikvm(2583): threadid=11: thread exiting uncaught exception (group=0x41b132b8) 06-27 18:13:28.180: e/androidruntime(2583): fatal exception: asynctask #1 06-27 18:13:28.180: e/androidruntime(2583): java.lang.runtimeexception: error occured while executing doinbackground() 06-27 18:13:28.180: e/androidruntime(2583): @ android.os.asynctask$3.done(asynctask.java:299) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.futuretask.run(futuretask.java:137) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 06-27 18:13:28.180: e/androidruntime(2583): @ java.lang.thread.run(thread.java:856) 06-27 18:13:28.180: e/androidruntime(2583): caused by: java.lang.nullpointerexception 06-27 18:13:28.180: e/androidruntime(2583): @ com.example.gpstracking.androidgpstrackingactivity$createnewproduct.doinbackground(androidgpstrackingactivity.java:131) 06-27 18:13:28.180: e/androidruntime(2583): @ com.example.gpstracking.androidgpstrackingactivity$createnewproduct.doinbackground(androidgpstrackingactivity.java:1) 06-27 18:13:28.180: e/androidruntime(2583): @ android.os.asynctask$2.call(asynctask.java:287) 06-27 18:13:28.180: e/androidruntime(2583): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 06-27 18:13:28.180: e/androidruntime(2583): ... 4 more 06-27 18:13:35.250: e/windowmanager(2583): activity com.example.gpstracking.androidgpstrackingactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@42554568 added here 06-27 18:13:35.250: e/windowmanager(2583): android.view.windowleaked: activity com.example.gpstracking.androidgpstrackingactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@42554568 added here 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.viewrootimpl.<init>(viewrootimpl.java:418) 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:322) 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:234) 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.windowmanagerimpl$compatmodewrapper.addview(windowmanagerimpl.java:153) 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.window$localwindowmanager.addview(window.java:559) 06-27 18:13:35.250: e/windowmanager(2583): @ android.app.dialog.show(dialog.java:300) 06-27 18:13:35.250: e/windowmanager(2583): @ com.example.gpstracking.androidgpstrackingactivity$createnewproduct.onpreexecute(androidgpstrackingactivity.java:109) 06-27 18:13:35.250: e/windowmanager(2583): @ android.os.asynctask.executeonexecutor(asynctask.java:586) 06-27 18:13:35.250: e/windowmanager(2583): @ android.os.asynctask.execute(asynctask.java:534) 06-27 18:13:35.250: e/windowmanager(2583): @ com.example.gpstracking.androidgpstrackingactivity$1.onclick(androidgpstrackingactivity.java:64) 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.view.performclick(view.java:4295) 06-27 18:13:35.250: e/windowmanager(2583): @ android.view.view$performclick.run(view.java:17456) 06-27 18:13:35.250: e/windowmanager(2583): @ android.os.handler.handlecallback(handler.java:615) 06-27 18:13:35.250: e/windowmanager(2583): @ android.os.handler.dispatchmessage(handler.java:92) 06-27 18:13:35.250: e/windowmanager(2583): @ android.os.looper.loop(looper.java:174) 06-27 18:13:35.250: e/windowmanager(2583): @ android.app.activitythread.main(activitythread.java:4952) 06-27 18:13:35.250: e/windowmanager(2583): @ java.lang.reflect.method.invokenative(native method) 06-27 18:13:35.250: e/windowmanager(2583): @ java.lang.reflect.method.invoke(method.java:511) 06-27 18:13:35.250: e/windowmanager(2583): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 06-27 18:13:35.250: e/windowmanager(2583): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 06-27 18:13:35.250: e/windowmanager(2583): @ dalvik.system.nativestart.main(native method) 06-27 18:13:35.290: d/dalvikvm(2583): gc_concurrent freed 189k, 10% free 9914k/11015k, paused 13ms+2ms, total 58ms
i have 1 code posted here if needs code please suggest me
package com.example.gpstracking; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import android.app.alertdialog; import android.app.service; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.asynctask; import android.os.bundle; import android.os.ibinder; import android.provider.settings; import android.util.log; public class gpstracker extends service implements locationlistener { private final context mcontext; // flag gps status boolean isgpsenabled = false; // flag network status boolean isnetworkenabled = false; // flag gps status boolean cangetlocation = false; location location; // location double latitude; // latitude double longitude; // longitude // minimum distance change updates in meters private static final long min_distance_change_for_updates = 10; // 10 meters // minimum time between updates in milliseconds private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute // declaring location manager protected locationmanager locationmanager; public gpstracker(context context) { this.mcontext = context; getlocation(); } public location getlocation() { try { locationmanager = (locationmanager) mcontext .getsystemservice(location_service); // getting gps status isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { this.cangetlocation = true; if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } // if gps enabled lat/long using gps services if (isgpsenabled) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps enabled", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } } catch (exception e) { e.printstacktrace(); } return location; } /** * stop using gps listener calling function stop using gps in * app * */ public void stopusinggps() { if (locationmanager != null) { locationmanager.removeupdates(gpstracker.this); } } /** * function latitude * */ public double getlatitude() { if (location != null) { latitude = location.getlatitude(); } // return latitude return latitude; } /** * function longitude * */ public double getlongitude() { if (location != null) { longitude = location.getlongitude(); } // return longitude return longitude; } /** * function check gps/wifi enabled * * @return boolean * */ public boolean cangetlocation() { return this.cangetlocation; } /** * function show settings alert dialog on pressing settings button * lauch settings options * */ public void showsettingsalert() { alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("gps settings"); // setting dialog message alertdialog .setmessage("gps not enabled. want go settings menu?"); // on pressing settings button alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent intent = new intent( settings.action_location_source_settings); mcontext.startactivity(intent); } }); // on pressing cancel button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); } @override public void onlocationchanged(location location) { } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public ibinder onbind(intent arg0) { return null; } } }
i'm not sure, maybe problem lies in this:
// building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("lat", lat)); params.add(new basicnamevaluepair("lng", lng));
in main actvity don't see set these 'lat' , 'lng' variables (there commented lines):
double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); // lat = double.tostring(latitude); // lng = double.tostring(longitude);
so 2 same, declared @ beginning:
string lat = ""; string lng = "";
beside that, have nullpointerexception in 131 line. please post it.
Comments
Post a Comment