本文介绍了如何解决此错误java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在行1列2路径$处为BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Android实现一个宁静的客户端.我有API URL,令牌等.我使用Retrofit 2库实现此应用程序,都是正确的.但是JSON值无法正确显示.我尝试了几种方法来解决此问题,但找不到任何正确的解决方案.
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.
这是我的代码:
JSON字符串
{"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}]}
主要活动
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();
}
});
}
用户类别
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;
}
}
存款课程
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;
}
}
界面类
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;
}}
推荐答案
我认为您的api接口类应为:
i think your api interface class should be :
@GET("api/deposits")
Call<User> getDeposits();
以及您的活动中:
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();
}
}
这篇关于如何解决此错误java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在行1列2路径$处为BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!