android - How to get malformed JSON in Retrofit 2 -
i sending request, getting exception, though request successful(the api interacting with, sends otp on success).
the exception is:
com.google.gson.stream.malformedjsonexception: use jsonreader.setlenient(true) accept malformed json @ line 1 column 2 path $ @ com.google.gson.stream.jsonreader.syntaxerror(jsonreader.java:1573) @ com.google.gson.stream.jsonreader.checklenient(jsonreader.java:1423) @ com.google.gson.stream.jsonreader.dopeek(jsonreader.java:575) @ com.google.gson.stream.jsonreader.peek(jsonreader.java:429) @ com.google.gson.internal.bind.typeadapters$13.read(typeadapters.java:349) @ com.google.gson.internal.bind.typeadapters$13.read(typeadapters.java:346) @ com.google.gson.typeadapter.fromjson(typeadapter.java:256) @ retrofit.gsonconverter.frombody(gsonconverter.java:42) @ retrofit.okhttpcall.parseresponse(okhttpcall.java:144) @ retrofit.okhttpcall.access$000(okhttpcall.java:25) @ retrofit.okhttpcall$1.onresponse(okhttpcall.java:90) @ com.squareup.okhttp.call$asynccall.execute(call.java:168) @ com.squareup.okhttp.internal.namedrunnable.run(namedrunnable.java:33) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) @ java.lang.thread.run(thread.java:856)
now how see malformed json? in order know if json object malformed, if object being returned(which expecting string) , or object sending.
forgive me if trivial question, started android development, of week.
here service:
public static enrollmentapiinterface getapiclient(){ if (enrollmentrequest == null) { okhttpclient client = new okhttpclient(); client.interceptors().add(new interceptor() { @override public response intercept(chain chain) throws ioexception { response response = chain.proceed(chain.request()); request request = chain.request(); buffer buffer = new buffer(); request.body().writeto(buffer); string body = buffer.readutf8(); log.println(10, tag, body); log.i(tag, "hello: " + response); string bodystring = response.body().string(); log.i(tag, bodystring); response = response.newbuilder() .body(responsebody.create(response.body().contenttype(), bodystring)) .build(); return response; } }); gson gson = new gsonbuilder() .setdateformat("yyyy-mm-dd't'hh:mm:ssz") .create(); retrofit retrofit = new retrofit.builder() // .baseurl("http://10.0.2.2:6543/") // on avd .baseurl("http://192.168.0.106:6543") // on device .addconverterfactory(gsonconverterfactory.create(gson)) .build(); enrollmentrequest = retrofit.create(enrollmentapiinterface.class); } return enrollmentrequest; }
interface:
public interface enrollmentapiinterface { @headers({ "accept: application/json", "content-type: application/json" }) @post("auth/enroll") call<string> requestenrollment(@body jsonobject enrollmentdetails); @headers({ "accept: application/json", "content-type: application/json" }) @post("auth/enroll/auth") call<string> authoriseenrollment(@body jsonobject logindetails); } }
and here call:
enrollmentrequest request = new enrollmentrequest(); request.setmsisdn(msisdntxt.gettext().tostring()); request.setid_number(idnumbertxt.gettext().tostring()); enrollmentapiclient.enrollmentapiinterface service = enrollmentapiclient.getapiclient(); log.i(tag, "request: " + request.tojson()); call<string> call = service.requestenrollment(request.tojson()); call.enqueue(new callback<string>() { @override public void onresponse(response<string> response) { log.i(tag, "on response" + response); log.i(tag, "on response body" + response.body()); // create object of sharedpreferences. sharedpreferences sharedpref = preferencemanager.getdefaultsharedpreferences(that); //now editor sharedpreferences.editor editor = sharedpref.edit(); //put value editor.putstring("idnumber", idnumbertxt.gettext().tostring()); editor.putstring("msisdn", msisdntxt.gettext().tostring()); //commits edits editor.commit(); log.i(tag, "onclick-after"); intent intent = new intent(getapplicationcontext(), authoriseactivity.class); startactivity(intent); } @override public void onfailure(throwable t) { // comes in here log.i(tag, "nothere", t); log.d("callback", " throwable " + t.tostring()); toast.maketext(enrollactivity.this, "request failed", toast.length_long).show(); } });
just log network responses, see what's wrong.
@override public response intercept(chain chain) throws ioexception { response response = chain.proceed(chain.request()); log.w("retrofit@response", response.body().string()); return response; }
Comments
Post a Comment