简体   繁体   中英

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path$

I found some solutions but I really don't know how to start.

"Result": [
        {
            "id": 487749,
            "deliveryid": 71472,
            "salestransactiondetailsid": 680089,

Here is my code.

Interface

@GET("api/Ontrack/Delivery")
Call<List<Delivery>> getDeliveryDetails();

MainActivity

    call.enqueue(new Callback<List<Delivery>>() {
        @Override
        public void onResponse(Call<List<Delivery>> call, Response<List<Delivery>> response) {
            List<Delivery> get_delivery_details = response.body();

            String[] deliveryId = new String[get_delivery_details.size()];

            for (int i = 0; i<get_delivery_details.size(); i++) {
                deliveryId [i] = get_delivery_details.get(i).getDeliveryid();
            }

            listView.setAdapter(
                    new ArrayAdapter<>(
                            getApplicationContext(),
                            android.R.layout.simple_list_item_1,
                            deliveryId
            )
            );
        }

        @Override
        public void onFailure(Call<List<Delivery>> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

You will need 2 different classes based on the response you're getting.

class CustomResponse{
  private List<Delivery> Result;

  // getters and setters and constructors
}

class Delivery {
  // your current default class
}

Your interface changes to

@GET("api/Ontrack/Delivery")
Call<CustomResponse> getDeliveryDetails();

The current one was not working because it expected a list of Delivery items as a response but instead got an object called result as the first item ie, your response would have looked something like this :

{
  result:....
}

it expected

[.....]

Incase you do not want to create 2 classes then you'll need to modify the response such that it send back only the list without the Result: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM