Swapna
Swapna

Reputation: 127

Getting error java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

I am trying to fetch data in this format

Response:

{
    "data": [
        {
            "name": "SK NAYAB HALIMABI G MED",
            "gender": "0",
            "age": "22",
            "mobile": "2147483647",
            "address": "H.NO;5-59-2,NAYAB BAZAR, CHINNA TURAKAPALEM,522601"
        }
]
}  

This is my response class

public class ResponseDetails {

    @SerializedName("data")
    private List<DataResponse> mResult;

    public List<DataResponse> getResult() {
        return mResult;
    }

    public void setResult(List<DataResponse> result) {
        mResult = result;
    }
}

This is API class

public interface API {

    @POST("URL")
    @FormUrlEncoded
    Call<ResponseDetails> getDataDetails(@Field("getData") String getdata);
}

@SerializedName("data")
    private List<DataResponse> mResult;

    public List<DataResponse> getResult() {
        return mResult;
    }

    public void setResult(List<DataResponse> result) {
        mResult = result;
    }

I have requested using retrofit

String url = "url";
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request().newBuilder().
                addHeader("ApiKey",   "apikey")
                .build();
        return chain.proceed(request);
    }
});

OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(200, TimeUnit.SECONDS)
        .readTimeout(200, TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit.Builder builder = new Retrofit.Builder()
        .baseUrl(url)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit =  builder.build();
API apiservice=retrofit.create(API.class);
Call<ResponseDetails> call=apiservice.getDataDetails("1");
call.enqueue(new Callback<ResponseDetails>() {

    @Override
    public void onResponse(Call<ResponseDetails> call, retrofit2.Response<ResponseDetails> response) {
        if(response.isSuccessful()){
            ResponseDetails serverResponse = response.body();
            if (serverResponse != null) {
                //below is how you can get the list of result
                List<DataResponse> resultList = response.body().getResult();
            }

       System.out.println(  "repsonese)"+response.code());

           // List<DataResponse> dp = Arrays.asList(Objects.requireNonNull(response.body()).getData());
           // Log.e("dp", String.valueOf(dp.size()));
        }
    }

    @Override
    public void onFailure(@NonNull Call<ResponseDetails> call, @NonNull Throwable t) {
        t.printStackTrace();
        Log.e("TAG",t.toString());

    }
});

I am getting this error

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

Upvotes: 1

Views: 456

Answers (1)

Bill L.
Bill L.

Reputation: 11

Observation: That error happens when the return type of the call does not properly match the Json data. In this case, the Json response, in the outermost level, is a Json object and not a Json array (which is buried inside, as indicated by the []'s).

Action: Try calling with a JsonObject to catch it all, and then parsing down to the buried JsonArray afterwards, using your Response Details then.

Upvotes: 1

Related Questions