com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $
10,140
Can you try initializing Gson as below
Gson gson = new GsonBuilder()
.setLenient()
.create();
Possibly there could be an issue in the expected JSON.
Comments
-
Amit Sharma almost 2 years
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(); } }
-
Shadow Droid over 4 yearsCan you edit question and post
LoginUser
class. -
Md. Asaduzzaman over 4 yearsNeed to investigate
loginPost
andLoginUser
. Can you add those? -
Amit Sharma over 4 yearsI also use this way but it's also not working:- Gson gson = new Gson(); String result1 = String.valueOf(result); JsonReader reader = new JsonReader(new StringReader(result1)); reader.setLenient(true); String json = gson.fromJson(reader, UserInfo.class); JSONObject jObj = new JSONObject(json);
-
Sajith over 4 yearscan you post the error log?
-
Amit Sharma over 4 yearsE/error: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $ @Sajith
-
-
Vir Rajpurohit over 4 yearsThere can be issue in response content type. It should be
application/json
-
Amit Sharma over 4 yearsThank u for replies, but it's already application/json ("accept: application/json") @Vir Rajpurohit