Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
                    }
                });

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading