0

I'm new in Android development. This is my MainActivity.java. I already set the lenient and still receiving error D/onFailure: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

API returns: Its either Success or Validation like Error: FirstName is invalid.

private void saveUser() {
    showpDialog();

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

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("BASE_URL")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    APIService service = retrofit.create(APIService.class);
    User user = new User();

    user.setFirstName(editName.getText().toString());

    Call<User> call = service.saveUser(user.getFirstName());

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Response<User> response) {
            hidepDialog();
            Log.d("onResponse", "" + response.code() +
                    "  response body "  + response.body() +
                    " responseError " + response.errorBody() + " responseMessage " +
                    response.message());
        }

        @Override
        public void onFailure(Throwable t) {
            hidepDialog();
            Log.d("onFailure", t.toString());
        }
    });

}

Ive been searching and all of them says i need to setLenient to true, but I already did that. Any idea?

Joseph
  • 653
  • 1
  • 12
  • 28

1 Answers1

1

Either Success or Validation is not a valid JSON. I think that's why it's malformed. JSON is kind like this:

{ "status": "success" }

So, you should not parse it as JSON, but just read at as a raw string instead. Or, it's better to make your response from server become a valid JSON like above.

More more better to make it RESTful, that's read status from HTTP response status instead of response body, either it is 200 OK or other status code. But it's out of question, so for now just use JSON.

fikr4n
  • 3,250
  • 1
  • 25
  • 46