micsha123
micsha123

Reputation: 675

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

I'm using Retrofit 2.0 in my app. Everything was quite good, but when I started request, it returns:

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

Here's my API interface:

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

and model class:

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

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

Seems that problem is aroung gzip which enabled on the server side. But when I try code with Response - it works like a charm. So, problem is not in gzipping.

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?

Upvotes: 4

Views: 2252

Answers (2)

micsha123
micsha123

Reputation: 675

As Pankaj Kumar supposed - char[] iso is the problem. Changing type to String solved it!

Upvotes: 1

Paulo Avelar
Paulo Avelar

Reputation: 2140

EDIT: this answer is not valid for Retrofit 2+.
See what's new in Retrofit 2: http://inthecheesefactory.com/blog/retrofit-2.0/.


It may not help you in this case, but I've encountered the very same problem 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'd take it out.

Upvotes: 2

Related Questions