Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 $

like image 787
androidAnonDev Avatar asked Sep 06 '15 10:09

androidAnonDev


Video Answer


2 Answers

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

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

like image 149
giorgos.nl Avatar answered Oct 10 '22 05:10

giorgos.nl


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

like image 43
Rowan Avatar answered Oct 10 '22 05:10

Rowan