Java – Retrofit 2.0 java.lang.IllegalStateException : Expected BEGIN_ARRAY but was STRING

Retrofit 2.0 java.lang.IllegalStateException : Expected BEGIN_ARRAY but was STRING… here is a solution to the problem.

Retrofit 2.0 java.lang.IllegalStateException : Expected BEGIN_ARRAY but was STRING

I use Retrofit 2.0 in my app. Everything is fine, but when I start the request, it returns :

**java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at line 1 column 21 path $[0].iso**

This is my API interface:

public interface GetPhones {
    @GET("phones.php")
    Call<ArrayList<Phones>> getPhones();
}

and model classes:

public class Phones {
    int id;
    char[] iso;
    String name;
    String phone_1;
    String phone_2;
    String phone_3;
}

Code in fragment:

Retrofit retrofit = new Retrofit.Builder()
         .baseUrl(URL_API)
         .client(SSLSuppressClient.trustcert())
         .addConverterFactory(GsonConverterFactory.create())
         .build();
GetPhones getPhonesInfo = retrofit.create(GetPhones.class);
Call<GetPhones> call = getPhonesInfo.getPhones();
call.enqueue(new Callback<GetPhones>() {
    @Override
    public void onResponse(Response<GetPhones> response, Retrofit retrofit) {
        Toast.makeText(getActivity(), "Success!", Toast.LENGTH_SHORT).show();
    }

@Override
    public void onFailure(Throwable t) {
        Toast.makeText(getActivity(), "Failure!", Toast.LENGTH_SHORT).show();
        Log.d("LOG", t.getMessage());
                }
    });

It seems that the problem is with server-side enabled gzip. But when I try to write code using Response – it works like a charm. So, the problem is not compression.

Retrofit retrofit = new Retrofit.Builder()
             .baseUrl(URL_API)
             .client(SSLSuppressClient.trustcert())
             .addConverterFactory(GsonConverterFactory.create())
             .build();
GetPhones getPhonesInfo = retrofit.create(GetPhones.class);
Call<List<com.squareup.okhttp.Response>> call = getPhonesInfo.getPhones();
call.enqueue(new Callback<List<com.squareup.okhttp.Response>>() {
        @Override
        public void onResponse(Response response, Retrofit retrofit) {
             Toast.makeText(getActivity(), "Success!", Toast.LENGTH_SHORT).show();
             Log.d("LOG", response.message());
             Log.d("LOG", response.raw().toString());
}

@Override
        public void onFailure(Throwable t) {
             Toast.makeText(getActivity(), "Failed!", Toast.LENGTH_SHORT).show();
             Log.d("LOG", t.getMessage());
        }
});

Where did I go wrong?

Solution

EDIT: This answer is not valid for Retrofit 2+.
Check out what’s new in Retrofit 2: http://inthecheesefactory.com/blog/retrofit-2.0/ .


It may not help you in this case, but I had the exact same issue in one of my projects and the solution was to set the JSON converter to a custom instance of gson.

Add this to your Retrofit.Builder:

.setConverter(new GsonConverter(new Gson()))


EDIT: It may conflict with addConverterFactory(). Unless you need it and know why, I’ll take it out.

Related Problems and Solutions