解析JSON时发生错误。这是我的错误:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
我的JSON:
{
"statusCode":200,
"message":"success",
"result":[ {
"user_id":1, "title":"some title",
"address":"some address",
"from_hours":"09:00",
"to_hours":"20:00",
"min_price":5000,
},{
"user_id":4, "title":"some title",
"address":"some address",
"from_hours":"09:00",
"to_hours":"20:00",
"min_price":5000,
}
]
}
这是我的界面:
@GET(ApiEndpoint.SEARCH_PROVIDERS)
Single<Providers> searchProviders(@Query("name") String name);
我的网络请求:
RxSearchObservable.fromView(name)
.debounce(300, TimeUnit.MILLISECONDS)
.filter(new Predicate<String>() {
@Override
public boolean test(String text) throws Exception {
if (text.isEmpty()) {
return false;
} else {
Log.d("rx", "accept: not empty");
return true;
}
}
})
.distinctUntilChanged()
.switchMapSingle(new Function<String, Single<Providers>>() {
@Override
public Single<Providers> apply(String s) throws Exception {
Log.d("rx", "accept: switchMapSingle");
return getDataManager().getApiHelper().searchProviders(s)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Providers>() {
@Override
public void accept(Providers result) throws Exception {
Log.d("rx", "accept: subscribe");
getMvpView().onResultList(result.getResult());
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable)
throws Exception {
if (!isViewAttached()) {
return;
}
getMvpView().showMessage(AppMessages.ERROR_OCCURED);
Log.d("rx", "accept: "+throwable.getMessage());
getMvpView().hideLoading();
// handle the error here
if (throwable instanceof ANError) {
ANError anError = (ANError) throwable;
handleApiError(anError);
}
}
});
我不知道是什么引起了错误。
我的Providers.java类
public class Providers implements Parcelable {
@SerializedName("statusCode")
@Expose
private Integer statusCode;
@SerializedName("message")
@Expose
private String message;
@SerializedName("result")
@Expose
private Result result;
public final static Parcelable.Creator<Providers> CREATOR = new Creator<Providers>() {
@SuppressWarnings({
"unchecked"
})
public Providers createFromParcel(Parcel in) {
return new Providers(in);
}
public Providers[] newArray(int size) {
return (new Providers[size]);
}
}
;
protected Providers(Parcel in) {
this.statusCode = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.message = ((String) in.readValue((String.class.getClassLoader())));
this.result = ((Result) in.readValue((Result.class.getClassLoader())));
}
/**
* No args constructor for use in serialization
*
*/
public Providers() {
}
/**
*
* @param message
* @param statusCode
* @param result
*/
public Providers(Integer statusCode, String message, Result result) {
super();
this.statusCode = statusCode;
this.message = message;
this.result = result;
}
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(statusCode);
dest.writeValue(message);
dest.writeValue(result);
}
public int describeContents() {
return 0;
}
}
答案 0 :(得分:1)
您的POJO类期望的对象是结果,但是服务器中有数组来自
因此,您应像这样更改 MyProviders.java类
public class Providers implements Parcelable {
@SerializedName("statusCode")
@Expose
private Integer statusCode;
@SerializedName("message")
@Expose
private String message;
@SerializedName("result")
@Expose
private List<Result> result; // change this line
public final static Parcelable.Creator<Providers> CREATOR = new Creator<Providers>() {
@SuppressWarnings({
"unchecked"
})
public Providers createFromParcel(Parcel in) {
return new Providers(in);
}
public Providers[] newArray(int size) {
return (new Providers[size]);
}
};
protected Providers(Parcel in) {
this.statusCode = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.message = ((String) in.readValue((String.class.getClassLoader())));
this.result = ((Result) in.readValue((Result.class.getClassLoader())));
}
/**
* No args constructor for use in serialization
*/
public Providers() {
}
/**
* @param message
* @param statusCode
* @param result
*/
public Providers(Integer statusCode, String message, Result result) {
super();
this.statusCode = statusCode;
this.message = message;
this.result = result;
}
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(statusCode);
dest.writeValue(message);
dest.writeValue(result);
}
public int describeContents() {
return 0;
}
}