Jo Jo Roid Jo Jo Roid - 1 month ago 7
Android Question

Retrofit 2 Error BEGIN_ARRAY - Android?

I am using from

retrofit 2
in my project, in my project get me bellow error :

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


here is my
RequestInterface_ListAddress
:

public interface RequestInterface_ListAddress {
@GET("/courier.svc/allorderforcourier/{key}")
Call<JSONResponseAddressList> getJSONAddress(@Path("key") String key);
}


And :

public class JSONResponseAddressList {
private ModelAddressList[] AddressList;

public ModelAddressList[] getAddressList() {
return AddressList;
}

public void setAddressList(ModelAddressList[] addressList) {
AddressList = addressList;
}
}


And :

public class ModelAddressList {
private String Address;
private String Area;
private String ID;
private String Post_Payment;
private String Total_Off;
private String Total_Payable;
private String Total_Price;
private String X;
private String Y;
private String cDate;
private String cTime;
private Integer response;

public String getAddress() {
return Address;
}

public String getArea() {
return Area;
}

public String getID() {
return ID;
}

public String getPost_Payment() {
return Post_Payment;
}

public String getTotal_Off() {
return Total_Off;
}

public String getTotal_Payable() {
return Total_Payable;
}

public String getTotal_Price() {
return Total_Price;
}

public String getX() {
return X;
}

public String getY() {
return Y;
}

public String getcDate() {
return cDate;
}

public String getcTime() {
return cTime;
}

public Integer getResponse() {
return response;
}
}


In my activity class :

private void loadJSONObjectAddress(String key) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.8.11:8087/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface_ListAddress requestAddress = retrofit.create(RequestInterface_ListAddress.class);
Call<JSONResponseAddressList> callAddress = requestAddress.getJSONAddress(key);
callAddress.enqueue(new Callback<JSONResponseAddressList>() {
@Override
public void onResponse(Call<JSONResponseAddressList> call, Response<JSONResponseAddressList> response) {
JSONResponseAddressList JSA = new JSONResponseAddressList();
data = new ArrayList<>(Arrays.asList(JSA.getAddressList()));
for (int i = 0; i < data.size(); i++) {
Log.i("LOG", data.get(i).getcTime());
}
}

@Override
public void onFailure(Call<JSONResponseAddressList> call, Throwable t) {
Log.i("LOG", t.getMessage());
}
});
}


And my
Json
is :

[{"Address":"address",
"Area":"area",
"ID":"1395042722592466",
"Post_Payment":"15,000",
"Total_Off":"0",
"Total_Payable":"334,600",
"Total_Price":"334,600",
"X":"34.657805",
"Y":"50.878026",
"cDate":"2000\/04\/27",
"cTime":"22:59:23",
"response":0
},{
"Address":"address",
"Area":"area",
"ID":"13950706165751439",
"Post_Payment":"10,000",
"Total_Off":"0",
"Total_Payable":"11,700",
"Total_Price":"11,700",
"X":"34.659368",
"Y":"50.886652",
"cDate":"2000\/07\/06",
"cTime":"16:57:51",
"response":0
},{
"Address":"address",
"Area":"area",
"ID":"13950706165554790",
"Post_Payment":"10,000",
"Total_Off":"0",
"Total_Payable":"246,500",
"Total_Price":"246,500",
"X":"34.659368",
"Y":"50.886652",
"cDate":"2000\/07\/06",
"cTime":"16:55:54",
"response":0
}]

Answer

use "ArrayList" instead of "JSONResponseAddressList" Such as :

Call<ArrayList<ModelAddressList>> callAddress =  requestAddress.getJSONAddress(key);
callAddress.enqueue(new Callback<ArrayList<ModelAddressList>>() {
    @Override
    public void onResponse(Call<ArrayList<ModelAddressList>> call, Response<ArrayList<ModelAddressList>> response) {
     ....           
    }

    @Override
    public void onFailure(..) {
        //TODOS
    }
});

}

and In RequestInterface_ListAddress :

public interface RequestInterface_ListAddress {
    @GET("/courier.svc/allorderforcourier/{key}")
    Call<ArrayList<ModelAddressList>> getJSONAddress(@Path("key") String key);
}
Comments