java.lang.illegalstateException:预期BEGIN_OBJECT但是BEGIN_ARRAY Retrofit
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.lang.illegalstateException:预期BEGIN_OBJECT但是BEGIN_ARRAY Retrofit相关的知识,希望对你有一定的参考价值。
我收到错误java.lang.illegalstateException:预期BEGIN_OBJECT但是在第1行第42列BEGIN_ARRAY时,Call API改装,这是我的代码:
这是我的界面:
@FormUrlEncoded
@POST("myCollection")
Call<APIResponse<List<MyCollection>>> getMyCollection(
@Field("caller_id") String caller_id
);
这是电话:
public class UploadVideoToneActivity extends BaseActivity{
List<MyCollection> collection = new ArrayList<>();
private void loadCollection() {
ContactItem callerID = SessionManager.getProfile(this);
Call<APIResponse<MyCollection>> call = ServicesFactory.getService().getMyCollection(callerID.caller_id);
call.enqueue(new Callback<APIResponse<MyCollection>>() {
@Override
public void onResponse(Call<APIResponse<MyCollection>> call, Response<APIResponse<MyCollection>> response) {
if (response.isSuccessful() && response.body().isSuccessful()) {
List<MyCollection> data = (List<MyCollection>) response.body().data;
if (data != null) {
collection.clear();
collection.addAll(data);
rvCollection.getAdapter().notifyDataSetChanged();
}
}
else {
Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<APIResponse<MyCollection>> call, Throwable t) {
Toast.makeText(UploadVideoToneActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
这是来自API的格式JSON响应,这是来自API的格式JSON响应,
{
"code": 200,
"error_message": null,
"data": [
[
{
"caller_id": "44",
"content_id": "003",
"alias": "Eli Sugigi",
"judul": "Angkat sekarang juga",
"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"sub_start": "2017-12-27 14:17:10",
"sub_end": "2018-01-03 00:00:00"
}
],
[
{
"caller_id": "44",
"content_id": "002",
"alias": "Eli Sugigi",
"judul": "Mas Ganteng Angkat Dong",
"source_content":"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"thumb_pic":"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"sub_start": "2017-12-27 15:52:40",
"sub_end": "2018-01-03 00:00:00"
}
]
]
}
这是MyCollection类:
public class MyCollection implements Parcelable {
@SerializedName("content_id")
@Expose
public String content_id;
@SerializedName("alias")
@Expose
public String alias ;
@SerializedName("judul")
@Expose
public String judul;
@SerializedName("source_content")
@Expose
public String source_content;
@SerializedName("thumb_pic")
@Expose
public String thumb_pic;
@SerializedName("sub_start")
@Expose
public String sub_start ;
@SerializedName("sub_end")
@Expose
public String sub_end ;
protected MyCollection(Parcel in) {
content_id = in.readString();
alias = in.readString();
judul = in.readString();
source_content = in.readString();
thumb_pic = in.readString();
sub_start = in.readString();
sub_end = in.readString();
}
public static final Creator<MyCollection> CREATOR = new Creator<MyCollection>() {
@Override
public MyCollection createFromParcel(Parcel in) {
return new MyCollection(in);
}
@Override
public MyCollection[] newArray(int size) {
return new MyCollection[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(content_id);
parcel.writeString(alias);
parcel.writeString(judul);
parcel.writeString(source_content);
parcel.writeString(thumb_pic);
parcel.writeString(sub_start);
parcel.writeString(sub_end);
}
}
首先,你提供的json是不正确的:
这是正确的json:
{
"code": 200,
"error_message": null,
"data": [{
"caller_id": "44",
"content_id": "003",
"alias": "Eli Sugigi",
"judul": "Angkat sekarang juga",
"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"sub_start": "2017-12-27 14:17:10",
"sub_end": "2018-01-03 00:00:00"
},
{
"caller_id": "44",
"content_id": "002",
"alias": "Eli Sugigi",
"judul": "Mas Ganteng Angkat Dong",
"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"sub_start": "2017-12-27 15:52:40",
"sub_end": "2018-01-03 00:00:00"
}
]
}
稍后您的响应类将其用作响应类:
public class MyResponseClass
{
private String error_message;
private ArrayList<Data> data;
private String code;
public String getError_message ()
{
return error_message;
}
public void setError_message (String error_message)
{
this.error_message = error_message;
}
public ArrayList<Data> getData ()
{
return data;
}
public void setData ( ArrayList<Data> data)
{
this.data = data;
}
public String getCode ()
{
return code;
}
public void setCode (String code)
{
this.code = code;
}
@Override
public String toString()
{
return "ClassPojo [error_message = "+error_message+", data = "+data+", code = "+code+"]";
}
}
稍后使用此MyResponseData作为改装数据解析器:
ContactItem callerID = SessionManager.getProfile(this);
Call<MyResponseData> call = ServicesFactory.getService().getMyCollection(callerID.caller_id);
并在你的onResponse:
@Override
public void onResponse(Call<MyResponseData> call, Response<MyResponseData> response) {
if (response.isSuccessful() && response.body().isSuccessful()) {
//here collection will be arraylist of MyCollection class
collection.clear();
collection.addAll(response.body().getData());
rvCollection.getAdapter().notifyDataSetChanged();
}
else {
Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
}
}
测试它并在需要时进行微小的更改
问题是你在“JSONArray”中得到了回应,而你正试图在JSONObject
中收到回应。
您应该使用'List'作为getMyCollection(callerID.caller_id)
方法的返回类型。然后实现该方法。
在onResponse
方法中,您必须获得如下所示的列表
List<MyCollection> data = (List<MyCollection>) response.body()
更新
你可以访问这里(http://pojo.sodhanalibrary.com/)
把你的JSON
回复,然后按提交。你将获得Pojo类结构。
尝试从APIResponse<MyCollection>
更改为APIResponse<List<MyCollection>>
所以代码变成:
public class UploadVideoToneActivity extends BaseActivity{
List<MyCollection> collection = new ArrayList<>();
private void loadCollection() {
ContactItem callerID = SessionManager.getProfile(this);
Call<APIResponse<List<MyCollection>>> call = ServicesFactory.getService().getMyCollection(callerID.caller_id);
call.enqueue(new Callback<APIResponse<List<MyCollection>>>() {
@Override
public void onResponse(Call<APIResponse<List<MyCollection>>> call, Response<APIResponse<List<MyCollection>>> response) {
if (response.isSuccessful() && response.body().isSuccessful()) {
List<MyCollection> data = (List<MyCollection>) response.body().data;
if (data != null) {
collection.clear();
collection.addAll(data);
rvCollection.getAdapter().notifyDataSetChanged();
}
}
else {
Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<APIResponse<List<MyCollection>>> call, Throwable t) {
Toast.makeText(UploadVideoToneActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
还要将您的Json(部分)更新为:
"data": [{
"caller_id": "44",
"content_id": "003",
"alias": "Eli Sugigi",
"judul": "Angkat sekarang juga",
"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"sub_start": "2017-12-27 14:17:10",
"sub_end": "2018-01-03 00:00:00"
},
{
"caller_id": "44",
"content_id": "002",
"alias": "Eli Sugigi",
"judul": "Mas Ganteng Angkat Dong",
"source_content": "http://bla.bla.mp4",
"thumb_pic": "http://bla.bla.mp4",
"sub_start": "2017-12-27 15:52:40",
"sub_end": "2018-01-03 00:00:00"
}
]
希望能帮助到你!
以上是关于java.lang.illegalstateException:预期BEGIN_OBJECT但是BEGIN_ARRAY Retrofit的主要内容,如果未能解决你的问题,请参考以下文章