how to fix this error java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ [duplicate]

瘦欲@ 提交于 2019-12-13 10:19:23

问题


I'm implementing a restful client using Android. I have API URL, token, etc. I implement this application using Retrofit 2 library, all are correct. But the JSON values do not display properly. I've tried several ways to solved this problem but can't find any correct solution for this.

This is my code:

JSON string

{"deposits":[{"id":806638,"amount":100,"status":"Canceled","reason":null},{"id":814436,"amount":1,"status":"Approved","reason":null},{"id":814452,"amount":1,"status":"Approved","reason":null},{"id":814505,"amount":1,"status":"Approved","reason":null}]}

Main Activity

void getRetrofitArray() {

    final String API_BASE_URL = "https://www.example.com/";
    final String credentials = "Bearer dfdfdfdlcdvksbdjsbdlvffdfddfdfdfjloiulfjuktj92p0JTFwJuFHlObIlWn8feQ_WA";
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new okhttp3.Interceptor() {
                @Override
                public Response intercept(okhttp3.Interceptor.Chain chain) throws IOException {

                    Request request = chain.request().newBuilder()
                            .addHeader("Authorization", credentials)
                            .addHeader("Accept", "application/json").build();

                    return chain.proceed(request);

                }
            }).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    LoginApi service = retrofit.create(LoginApi.class);



    Call <List <Users>>call = service.getDeposits();
    Log.d("onResponse", "There is an xlllllllllllllllllllllkkkkkkklll");
    call.enqueue(new Callback<List<Users>>(){
        @Override
        public void onResponse(Call<List<Users>>call, retrofit2.Response<List<Users>>response) {

            try {

              // Users   UserData = response.body();
                List <Users> UserData =response.body();

                //String jsonString= response.body().toString();
               // Type listType = new TypeToken<List<Users>>(){}.getType();

                                      for (int i = 0; i < UserData.size(); i++) {

                     text_marks_1.setText("StudentMarks  : " +UserData.get(i).getDeposits());


                   // else if (i == 2) {
                        //text_id_2.setText("StudentId  :  " + UserData.get(i).getName());
                   //
               }
            } catch (Exception e) {
                Log.d("onResponse", "There is an error");
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<List<Users>>call, Throwable t) {
            Log.d("onResponse", "There is an error");
            t.printStackTrace();
        }
    });

}

Users Class

public class Users {
    @SerializedName("deposits")
    @Expose
    private List<Deposit> deposits = new ArrayList<Deposit>();

    /**
     *
     * @return
     * The deposits
     *
     */
    public List<Deposit> getDeposits() {
        return deposits;
    }

    /**
     *
     * @param deposits
     * The deposits
     */
    public void setDeposits(List<Deposit> deposits) {
        this.deposits = deposits;
    }

}

Deposits Class

public class Deposit {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("amount")

    @Expose
    private Integer amount;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("reason")
    @Expose
    private Object reason;

    /**
     *
     * @return
     * The id
     */
    public Integer getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The amount
     */
    public Integer getAmount() {
        return amount;
    }

    /**
     *
     * @param amount
     * The amount
     */
    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    /**
     *
     * @return
     * The status
     */
    public String getStatus() {
        return status;
    }

    /**
     *
     * @param status
     * The status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     *
     * @return
     * The reason
     */
    public Object getReason() {
        return reason;
    }

    /**
     *
     * @param reason
     * The reason
     */
    public void setReason(Object reason) {
        this.reason = reason;
    }

}

interface class

public interface LoginApi {

    //@GET("/media/webservice/JsonReturn.php HTTP/1.1")
    // Call<List<User>> getUserDetails();


    @GET("api/deposits")
    Call <List<Users>> getDeposits();
}

edited...... new Deposit class

public class Deposit implements Serializable {

@SerializedName("deposits")
@Expose
private List<Deposit> deposits ;   

public List<Deposit> getDeposits() {
    return deposits;
}

public void setDeposits(List<Deposit> deposits) {
    this.deposits = deposits;
}

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("amount")
@Expose
private Integer amount;
@SerializedName("status")
@Expose
private String status;
@SerializedName("reason")
@Expose
private Object reason;

public Integer getId() {
    return id;
}


public void setId(Integer id) {
    this.id = id;
}


public Integer getAmount() {
    return amount;
}


public void setAmount(Integer amount) {
    this.amount = amount;
}


public String getStatus() {
    return status;
}


public void setStatus(String status) {
    this.status = status;
}

public Object getReason() {
    return reason;
}


public void setReason(Object reason) {
    this.reason = reason;
}}

回答1:


i think your api interface class should be :

@GET("api/deposits")
Call<User> getDeposits();

and in your Activity :

Call<User> call = service.getDeposits();
call.enqueue(new Callback<User>(){
    @Override
    public void onResponse(Call<User>call, retrofit2.Response<User>response) {

            List<Deposit> = response.getDeposits();

           }

    }


来源:https://stackoverflow.com/questions/40837031/how-to-fix-this-error-java-lang-illegalstateexception-expected-begin-array-but

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!