here mainactivity class project trying call json formatted link has forecast info:
public class mainactivity extends actionbaractivity { public static final string tag = mainactivity.class.getsimplename(); private currentweather mcurrentweather; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string apikey = "bf092d4688e81db384b589d7a225e061"; double apilat = 37.8267; double apilong = -122.423; string forecasturl = "https://api.forecast.io/forecast/" + apikey + "/" + apilat + "," + apilong; if (isnetworkonline()) { okhttpclient client = new okhttpclient(); request request = new request.builder().url(forecasturl).build(); call call = client.newcall(request); call.enqueue(new callback() { @override public void onfailure(request request, ioexception e) { } @override public void onresponse(response response) throws ioexception { try { string jsondata = response.body().tostring(); log.v(tag, response.body().string()); if (response.issuccessful()) { mcurrentweather = getcurrentdetails(jsondata); } else { alertuserabouterror(); } } catch (ioexception | jsonexception e) { log.e(tag, "caught exception: ", e); } } }); } else { toast.maketext(this, getstring(r.string.network_not_available), toast.length_long).show(); } log.d(tag, "main ui code running."); } private currentweather getcurrentdetails(string jsondata) throws jsonexception { // getting first json object in json formatted link. jsonobject forecast = new jsonobject(jsondata); string timezone = forecast.getstring("timezone"); // getting second json object in json format looking // keyword "currently" jsonobject = forecast.getjsonobject("currently"); currentweather currentweather = new currentweather(); currentweather.sethumidity(currently.getdouble("humidity")); currentweather.seticon(currently.getstring("icon")); currentweather.setprecipchance(currently.getdouble("precipprobability")); currentweather.setsummary(currently.getstring("summary")); currentweather.settemperature(currently.getdouble("temperature")); currentweather.settime(currently.getlong("time")); currentweather.settimezone(timezone); // displaying formatted time seconds format // specified. toast.maketext(mainactivity.this, currentweather.getformattedtime(), toast.length_long).show(); return currentweather; } private boolean isnetworkonline() { connectivitymanager manager = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo networkinfo = manager.getactivenetworkinfo(); if (networkinfo != null && networkinfo.isconnected()) { return true; } else { return false; } } } however, error keeps popping , i'm unsure error saying , why occurring.
07-21 01:37:10.644 20859-20908/com.example.android.stormy e/mainactivity﹕ caught exception: org.json.jsonexception: value com.squareup.okhttp.internal.http.realresponsebody@423e87e0 of type java.lang.string cannot converted jsonobject @ org.json.json.typemismatch(json.java:111) @ org.json.jsonobject.<init>(jsonobject.java:158) @ org.json.jsonobject.<init>(jsonobject.java:171) @ com.example.android.stormy.mainactivity.getcurrentdetails(mainactivity.java:73) @ com.example.android.stormy.mainactivity.access$100(mainactivity.java:22) @ com.example.android.stormy.mainactivity$1.onresponse(mainactivity.java:54) @ com.squareup.okhttp.call$asynccall.execute(call.java:170) @ com.squareup.okhttp.internal.namedrunnable.run(namedrunnable.java:33) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:856)
java.lang.string cannot converted jsonobject @ org.json.json.typemismatch(json.java:111) this appears because web service using not returning jsonobject
it may happen when key sending web service incorrect or may happens if key value duplicate primary key in database or may web service causes that.
possible solutions:
make sure keys correct , match web service keys, debug , find out returned json string know if duplicate , change value trying send, make sure web service returns valid value after changing get , send params through url.
or may caused un-wanted characters added when compose string if so; chick out this.
Comments
Post a Comment