1

I have a problem with Retrofit & Android.

I have API method "api/account/login". This method has Content-Type application/json and what about the body:

"Request body has to contain LoginData object serialized to JSON format and properly encrypted" (from api doc) So, sample body:

"cUdu2LkEChP...Lw7R9m4="

Also, I have second API Method: "api/account/data", which have the same body and content-type.

What is my problem? I am using Retrofit to connect with API.

interface APIService {
@POST("api/account/login")
void postLogin(@Body String encryptedCredentials, Callback<String> callback);
@POST("/api/account/data")
void postLoginData(@Body String encryptedCredentials, Callback<String> response);
}

for first method everything works fine, API returns what should return, but, for the second method Retrofit returns error:

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

Actually now I have no idea what is going on, both methods take the same body, but only one is failing...

handling api-calls:

@Override
// THIS WORKS FINE!
public APIRequest postLoginRequest(final String encryptedCredentials) {
    return new APIRequest() {
        @Override
        protected void fireRequest() {
            APIServiece.postLogin(encryptedCredentials, new Callback<String>() {
                @Override
                public void success(String s, Response response) {
                    logicEventBus.post(new LoginEvent(null, s));
                }

                @Override
                public void failure(RetrofitError error) {
                    logicEventBus.post(new LoginEvent(error, null));
                }
            });
        }
    };
}

@Override
// THIS DOESNT WORK FINE :(
public APIRequest postLoginDataRequest(final String encryptedCredentials, final AccountData.LoggedBy loggedBy) {
    return new APIRequest() {
        @Override
        protected void fireRequest() {
            Callback<String> callback = new Callback<String>() {
                @Override
                public void success(String s, Response response) {
                    logicEventBus.post(new LoginDataEvent(null, s));
                }

                @Override
                public void failure(RetrofitError error) {
                    logicEventBus.post(new LoginDataEvent(error, null));
                }
            };

            switch (loggedBy) {
                case INTERNAL:
                    APIService.postLoginData(encryptedCredentials, callback);
                    break;
                case FACEBOOK:
                    APIService.postLoginData(encryptedCredentials, callback);
                    break;
                case GOOGLEPLUS:
                    APIService.postLoginData(encryptedCredentials, callback);
                    break;
            }
        }
    };
}

I have checked in the debugger, strings are fine. API also works fine (checked in Postman). Debugger never get into "success" (in not-working method).

Thanks for any kind of help!

5
  • Possible duplicate of GSON: Expected a string but was BEGIN_OBJECT? Commented Aug 29, 2017 at 9:44
  • Look at this: stackoverflow.com/questions/32069021/… Commented Aug 29, 2017 at 9:45
  • Can you put "/api/account/data" API Response So i better understand your concern. Commented Aug 29, 2017 at 9:46
  • I cant even make Api call in postLoginDataRequest. I dont have problem with parsing string using gson..... Commented Aug 29, 2017 at 9:54
  • This is normal parsing exception occurred when your Pojo key data type is different from json response. Check POJO you have created for Login and PostLogin response. Commented Aug 29, 2017 at 11:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.