1

I am getting error like

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 7 path $

And I tried 1 answer of this MalformedJsonException with Retrofit API?1, I am using retrofit 1.8, what should I do?

My Java code:

RetroHelper

public static RestAdapter getAdapter(Context ctx, String serverUrl) {
        mContext=ctx;
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(StringConstants.BASE_CLASSES_URL+serverUrl)
                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new RestAdapter.Log() {
                    @Override
                    public void log(String msg) {
                        Log.i("Retro Helper", msg);
                    }
                })
                .build();

JSON response

{
    "status": "Success",
    "data": null,
    "message": "Successfully get the slot lists",
    "statusCode": 200
}

other code

public ServiceOperations getBaseClassService(Context ctx, String url) {
            return new RetroHelper().getAdapter(ctx, url).create(ServiceOperations.class);
        }



     @GET("/schduleLists")
        void getDoctorSchedule(@Query("doctorid") int id, Callback<JsonObject> callback);

 mUtil.getBaseClassService(getActivity(), "").getDoctorSchedule(mUserId,new Callback<JsonObject>() {
                @Override
                public void success(JsonObject jsonObject, Response response) {
                    if (jsonObject.get(StringConstants.STATUS).getAsString().equalsIgnoreCase("Success")) {// this if condition is casued error!
                        mUtil.dismissDialog();
                        JsonArray jsonArray =jsonObject.get("data").getAsJsonArray();
Community
  • 1
  • 1
Lokesh
  • 316
  • 3
  • 14

1 Answers1

0

I suspect that result1 has some characters at the end of it that you can't see in the debugger that follow the closing } character. What's the length of result1 versus result2? I'll note that result2 as you've quoted it has 169 characters.

GSON throws that particular error when there's extra characters after the end of the object that aren't whitespace, and it defines whitespace very narrowly (as the JSON spec does) - only \t, \n, \r, and space count as whitespace. In particular, note that trailing NUL (\0) characters do not count as whitespace and will cause this error.

If you can't easily figure out what's causing the extra characters at the end and eliminate them, another option is to tell GSON to parse in lenient mode:

Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(result1));
reader.setLenient(true);
Userinfo userinfo1 = gson.fromJson(reader, Userinfo.class);
  • Sorry for the delay,the php response was unclear,it has raw data with json data, next I took clear response from good API,so that my problem was clear. – Lokesh Jan 28 '17 at 07:39