6

I created Retrofit interface

public interface UserService {
@GET(Constants.Api.URL_LOGIN)
Call<String> loginUser(@Query("email") String email, @Query("password") String pass, @Query("secret") String secret, @Query("device_id") String deviceid, @Query("pub_key") String pubkey, @Query("device_name") String devicename);

When in Activity I call

final Call<String> responce = 
  service.loginUser(loginedt.getText().toString(), md5(passwordedt.getText().toString()), secret, device_id, pub_key, device_name);

responce.enqueue(new Callback<String>() {
    @Override
    public void onResponse(Response<String> response) {
        if (response.code() == Constants.Status.ERROR_404) {
            Toast.makeText(LoginActivity.this, getResources().getString(R.string.wrong_log_pass), Toast.LENGTH_LONG).show();
        } else if (response.code() != Constants.Status.ERROR_404 && response.code() != Constants.Status.SUCCES) {
            Toast.makeText(LoginActivity.this, getResources().getString(R.string.wrong_request), Toast.LENGTH_LONG).show();
        } else {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        }
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
                       
    }
});

I am getting error

onFailure java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $

3
  • Usually these kind of errors are because of GSon can't parse your json from your objects. Post your models for User too. Let's see if everything is correct Commented Sep 6, 2015 at 10:51
  • I don't post any objects. @Query Strings only Commented Sep 6, 2015 at 11:12
  • I'm not sure 100% but I think that you will need an entity class to work with retrofit. The reason is that internally it uses Gson and if you don't have any entity class Retrofit does not know how to parse POST,GET data Commented Sep 6, 2015 at 11:27

3 Answers 3

12

If you don't create your own models, use Call<JsonObject>.

This is the default parser for application/json content type requests.

Sign up to request clarification or add additional context in comments.

Comments

4

What is the response of your API? Retrofit parses the server response into the type passed to call object, i.e. Call<ResponseType>. As from error you are receiving, server returns object while you're expecting a string.
change your service to be

Call<ResponseTypeObject> loginUser(@Query("email") String email, @Query("password") String pass, @Query("secret") String secret, @Query("device_id") String deviceid, @Query("pub_key") String pubkey, @Query("device_name") String devicename);

where ResponseTypeObject is the response entity as returned from server

Comments

3

Such error occurs whenever the Gson converter fails to parse the Json object from the server to the expected return type(in your case, which is a String). In such case, change the return type from Call<String> to Call<ResponseBody>.

Comments

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.