0

I have a problem when parsing json by using Retrofit can't get any of data , This URL request array of parameter https://lao.busnavi.asia/api/location.php?ids%5B%5D=132&ids%5B%5D=133&ids%5B%5D=131

JSON from server that looks like this json data

My code

LatestBusLocation.java

@SerializedName("id") private int id;
@SerializedName("status") private int status;
@SerializedName("route_id") private String routeId;
@SerializedName("lng") private double lng;
@SerializedName("lat") private double lat;
@SerializedName("accuracy") private double accuracy;
@SerializedName("speed") private double speed;
@SerializedName("heading") private float heading;
@SerializedName("date") private Date date;

Interface class

public interface ApiService {
@POST("location.php")
Call<LatestBusLocation> loadLatestBusLocation(@Query("ids[]") int[] ids);

}

MainActivity.java

int[] ids = {131, 132, 133};

    Call<LatestBusLocation> call = HttpManager.getInstance()
            .getService().loadLatestBusLocation(ids);
    call.enqueue(new Callback<LatestBusLocation>() {
        @Override
        public void onResponse(Call<LatestBusLocation> call, Response<LatestBusLocation> response) {

            Log.e("WorkOrNote1", "Working");
            if (response.isSuccessful()) {
                dao = response.body();
                Log.d("daoResponse", String.valueOf(dao.getId()));

            } else {
                try {
                    Log.e("LatestBusLocation", response.errorBody().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<LatestBusLocation> call, Throwable t) {
            Log.e("onFailure", t.toString());
        }
    });

1 Answers1

0

Retrofit fails to map your object because you have specified an LatestBusLocation in the Call. This should be a List<LatestBusLocation>.

Aditionally, you will have to add a deserializer to map correctlly because you are getting an object (with ids keys) instead of a proper array. Look at this relationed answer

Community
  • 1
  • 1
crgarridos
  • 8,758
  • 3
  • 49
  • 61
  • I have used List but it don't work in logcat I get error message like this : onFailure: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ – Thanongsine Chantakham Oct 21 '16 at 01:32
  • I konow, because you are trying to get an array from the webservices. but you are getting an object, that has nested objects with their ids as keys. I updated the answer with a useful link – crgarridos Oct 21 '16 at 08:06