我想获取json从一个urlhttps://api.myjson.com/bins/mxcsl/使用改造和rxjava。
{
"data": [
{
"itemId": "1",
"desc": "Batcave",
"audio": "https://storage.googleapis.com/a/17.mp3"
},
{
"itemId": "2",
"desc": "Fight Club rules",
"audio": "https://storage.googleapis.com/a/2514.mp3"
},
{
"itemId": "3",
"desc": "Make an offer",
"audio": "https://storage.googleapis.com/a/47.mp3"
}]}
这是我的代码:数据模型:
public class Data {
private String itemId;
private String desc;
private String audio;
public String getItem() {
return itemId;
}
public String getDesc() {
return desc;
}
public String getAudio() {
return audio;
}}
这是接口:
public interface RequestInterface {
@GET("/bins/mxcsl/")
Observable<List<Data>> register();
}
我正在加载这样的内容:
private void loadJSON() {
RequestInterface requestInterface = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(RequestInterface.class);
mCompositeDisposable.add(requestInterface.register()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(this::handleResponse,this::handleError));
}
private void handleResponse(List<Data> androidList) {
mAndroidArrayList = new ArrayList<>(androidList);
mAdapter = new DataAdapter(mAndroidArrayList);
mRecyclerView.setAdapter(mAdapter);
}
private void handleError(Throwable error) {
Toast.makeText(this, "Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
但是我也得到了预期的错误BEGIN_ARRAY但被BEGIN_OBJECT
我不知道这是哪里出了问题。请帮忙。
您的类型
Observable<List<Data>> register();
是错误的。因为Json的第一级不是Array(而是字段<code>data</code>的对象,即Array)。您应该为外部结构创建类
public class Outer{
List<Data> data;
}
并在改造中指定,如可观察:
@GET("/bins/mxcsl/")
Observable<Outer> register();
您的 api 响应是一个 JSONObject
,其中包含一个带有关键数据的
JSONArray
。
用另一个Model类包装数据列表
public class ResponseData {
@SerializedName("data")
private List<Data> dataList;
public List<Data> getDataList() {
return dataList;
}
public void setDataList(List<Data> list) {
dataList = list;
}
}
使用RequseData
中的类RequestInterface
public interface RequestInterface {
@GET("/bins/mxcsl/")
Observable<ResponseData> register();
}
添加了:
由于您转换的响应是< code>ResponseData对象,因此您的< code>handleResponse方法参数将是< code>ResponseData类型。在< code>handleResponse中,从getter方法获取您期望的列表,并将其设置为< code>Adapter。
<code>handleResponse</code>方法
private void handleResponse(ResponseData responseData) {
if( responseData != null){
List<Data> dataList = responseData.getDataList();
mAndroidArrayList = new ArrayList<>();
if(dataList!= null){
mAndroidArrayList.addAll(dataList);
}
mAdapter = new DataAdapter(mAndroidArrayList);
mRecyclerView.setAdapter(mAdapter);
}
}
错误是因为Retrofit期望将json数组反序列化为List
public interface RequestInterface {
@GET("/bins/mxcsl/")
Observable<List<Data>> register();
}
但是json响应是一个对象,该对象具有一个名为data的字段,该字段是一个数组
{
"data": [
{
"itemId": "1",
"desc": "Batcave",
"audio": "https://storage.googleapis.com/a/17.mp3"
},
{
"itemId": "2",
"desc": "Fight Club rules",
"audio": "https://storage.googleapis.com/a/2514.mp3"
},
{
"itemId": "3",
"desc": "Make an offer",
"audio": "https://storage.googleapis.com/a/47.mp3"
}]}