java - Android Exception in parsing a static json -
i have configuration file pick server @ launch of android app. code fetching file pretty standard
@override protected void onresume() { super.onresume(); new connectiontask().execute(); } private class connectiontask extends asynctask<string, string, string> { @override protected void onpreexecute() { } @override protected string doinbackground(string... args) { string result = null; authenticateconnection mauth1 = new authenticateconnection(); try { result = mauth1.connection(); } catch (exception e) { e.printstacktrace(); } return result; } @override protected void onpostexecute(string str) { if (!isfinishing() && !isdestroyed()) { if (!textutils.isempty(str)) { log.e("str", "" + str); /****** below line crashes*******/ baseurl obj = new gson().fromjson(str, baseurl.class); /****** above line crashes*******/ //some logic here } } } } private class authenticateconnection { public authenticateconnection() { } public string connection() throws exception { string str = ""; try { // create url desired page if (!textutils.isempty(getapplicationcontext().getresources().getstring(r.string.configuration_url))) { string aurl = getapplicationcontext().getresources().getstring(r.string.configuration_url); url url = new url("http://" + aurl); str = convertstreamtostring(url.openstream()); } else { str = ""; } } catch (malformedurlexception e) { } catch (ioexception e) { } return str; } public string convertstreamtostring(inputstream is) { bufferedreader reader = null; try { reader = new bufferedreader(new inputstreamreader(is, "utf-8")); } catch (unsupportedencodingexception e) { e.printstacktrace(); } stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } }
i getting strange exception while parsing file. , team have not encountered error in our unit testing crashlytics reports lot of user facing exception, here in onpostexecute(), while parsing json, baseurl obj = new gson().fromjson(str, baseurl.class);
what potential reasons exception.
fatal exception: com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object string @ line 1 column 1 path $ @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(sourcefile:200) @ com.google.gson.gson.fromjson(sourcefile:810) @ com.google.gson.gson.fromjson(sourcefile:775) @ com.google.gson.gson.fromjson(sourcefile:724) @ com.google.gson.gson.fromjson(sourcefile:696) @ com.myapp.activity.splash$connectiontask.onpostexecute(sourcefile:263) @ com.myapp.activity.splash$connectiontask.onpostexecute(sourcefile:237) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5095) @ java.lang.reflect.method.invokenative(method.java) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602) @ dalvik.system.nativestart.main(nativestart.java) caused java.lang.illegalstateexception: expected begin_object string @ line 1 column 1 path $ @ com.google.gson.stream.jsonreader.beginobject(sourcefile:387) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(sourcefile:189) @ com.google.gson.gson.fromjson(sourcefile:810) @ com.google.gson.gson.fromjson(sourcefile:775) @ com.google.gson.gson.fromjson(sourcefile:724) @ com.google.gson.gson.fromjson(sourcefile:696) @ com.myapp.activity.splash$connectiontask.onpostexecute(sourcefile:263) @ com.myapp.activity.splash$connectiontask.onpostexecute(sourcefile:237) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5095) @ java.lang.reflect.method.invokenative(method.java) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602) @ dalvik.system.nativestart.main(nativestart.java)
the reference json is stored in configuration file below, , baseurl.java conforms json:
{ "baseurls": [{ "versioncode": 10, "baseurl": "http://myapp.in", "infra": "staging" },{ "versioncode": 11, "baseurl": "http://myapp.in", "infra": "production" }] }
adding baseurl.java question:
public class baseurl implements parcelable { arraylist<versioncontrol> baseurls; public arraylist<versioncontrol> getbaseurls() { return baseurls; } public void setbaseurls(arraylist<versioncontrol> baseurls) { this.baseurls = baseurls; } protected baseurl(parcel in) { if (in.readbyte() == 0x01) { baseurls = new arraylist<versioncontrol>(); in.readlist(baseurls, versioncontrol.class.getclassloader()); } else { baseurls = null; } } @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { if (baseurls == null) { dest.writebyte((byte) (0x00)); } else { dest.writebyte((byte) (0x01)); dest.writelist(baseurls); } } @suppresswarnings("unused") public static final parcelable.creator<baseurl> creator = new parcelable.creator<baseurl>() { @override public baseurl createfromparcel(parcel in) { return new baseurl(in); } @override public baseurl[] newarray(int size) { return new baseurl[size]; } }; }
expected begin_object string @ line 1 column 1 path $
in case means expecting see {
- start of object, instead saw string, sort of error warning webserver spurts out.
imagine android client receiving response message such
"you unauthorized make request"
instead of receiving valid json, that's cause here.
Comments
Post a Comment