Amit Sharma
Amit Sharma

Reputation: 281

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $

During Login time i got this error, please don't downgrade this if you know any solution please guide me. I also checked different stack overflow post but they all are talking about trim the string or other solution with retrofit but i can't solve it. I use volley.

My Json File:-

{
  "error": false,
  "user": [
    {
      "user_id": "123",
      "customer_name": "Abc",
      "email": "[email protected]",
      "salt": "123abc",
      "phone": "1234567890",
      "address": "Enter Address",
      "postal_code": "1234"
    }
  ]
}

This is my Java Class :-

@Override
    protected String doInBackground(Boolean... booleen) {
        ExampleApi exampleApi = new ExampleApi();
        LoginUser result;
        try {
            result = exampleApi.loginPost(sharedPreferences.getString("abc_id", ""), semail, fcmToken, spassword);
            Gson gson = new Gson();                           
            String json = gson.toJson(result);
            JSONObject jObj = new JSONObject(json);
            Log.e("result1", String.valueOf(jObj));
            if (jObj.getString("error").equalsIgnoreCase("false")) {
                JSONArray jsonArray = jObj.getJSONArray("user");
                JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                 return "true";
                 }else {
                String errormsg = jObj.getString("error_msg");
                return errormsg;
            }
            } catch (ApiException e) {
            e.printStackTrace();
            Log.e("error", e.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            }
            }

Upvotes: 2

Views: 20762

Answers (3)

Viktor Chmel
Viktor Chmel

Reputation: 144

In my case, this error was generated because of non UTF-8 json file encoding. When I convert it to UTF-8 all started to work.

Upvotes: 0

Sajith
Sajith

Reputation: 771

Check whether the properties of user object (user_id, customer_name. etc) are in correct format which is equal to the object received as response.

ps: If user_id is an Integer in your user object also it should be an Integer

Upvotes: 0

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

Can you try initializing Gson as below

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Possibly there could be an issue in the expected JSON.

Upvotes: 1

Related Questions