Skip to content
Advertisement

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message

i am making login function in android using retrofit. I have created an endpoint for login validation, then I have tested it using Postman using raw (json) and it worked. But when I enter the endpoint into android using retrofit I get an error message like this:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message

can anyone help me?

So here my source:

ApiClient

public class ApiClient {

    public static final String BASE_URL = "";
    public static Retrofit retrofit;

    public static Retrofit getRetrofit() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }

}

AuthInterface

public interface AuthInterface {

    @Headers("Content-Type: application/json")
    @POST("auth/login")
    Call<AuthPost> authPostCall(@Body String body);
}

AuthPost

public class AuthPost {

    @SerializedName("status")
    private String status;
    @SerializedName("error_code")
    private int error_code;
    @SerializedName("message")
    private String message;
    @SerializedName("token")
    private String token;

    ...getter and setter
}

LoginActivity

                JSONObject payload = new JSONObject();
                try {
                    payload.put("login_username", loginUsernameText);
                    payload.put("login_password", loginPasswordText);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Call<AuthPost> authPostCall = authInterface.authPostCall(payload.toString());
                authPostCall.enqueue(new Callback<AuthPost>() {
                    @Override
                    public void onResponse(Call<AuthPost> call, Response<AuthPost> response) {
                        if (response.code() == 200) {
                            
                        } else {
                            
                        }
                    }

                    @Override
                    public void onFailure(Call<AuthPost> call, Throwable t) {
                        t.printStackTrace();
                    }
                });

Advertisement

Answer

Are you sure about:

@SerializedName("message")
    private String message;

Usually this error appears if this field is Object. Does your JSON looks like

"message":"test"

or something like:

"message":{"field":"value"}

If it is the second variant so you should simple change the field to necessary type.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement