RxJava2和Retrofit2:预期BEGIN_ARRAY但在第1行第2列路径$ BEGIN_OBJECT $

问题描述 投票:0回答:2

我一直在尝试如何使用RxJava2和Retrofit 2来使用Github API。试图访问链接:<https://api.github.com/search/repositories?q=topic:ruby+topic:rails>。我想显示NAME和NODE_ID。但一直坚持错误:

java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$为BEGIN_OBJECT。

这是Retrofit的Client类。

    public class Client {
public static final String GITHUB_BASE_URL = "https://api.github.com/search/";
private static Client instance;
private static GetData data;
public Client() {
    final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.
            LOWER_CASE_WITH_UNDERSCORES).create();
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(GITHUB_BASE_URL).
            addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).build();
    data = retrofit.create(GetData.class);
}
public static Client getInstance(){
    if(instance==null)
        instance = new Client();
    return  instance;
}

public Observable<List<itemsClass>> getAllUsers(){
    return data.getAllUsers();
}

界面

    @GET("repositories?q=topic:ruby+topic:rails")
Observable<List<itemsClass>> getAllUsers();        

我需要通过调用获得的数据是这样的:

    {
     "total_count": 2997,
     "incomplete_results": false,
     "items": [
          {
              "id": 8514,
              "name": "rails",
               "owner": {  
                     "node_id": "MDEyOk9yZ2FuaXphdGlvbjQyMjM=" 
                        },
             }],
     },......     

还有我将显示数据的MainActivity。

    subscription = Client.getInstance().getAllUsers().
            subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.
            mainThread()).subscribe(new Observer<List<ParentClass>>() {
        @Override
        public void onCompleted() {
            Log.d(TAG,"In completed()");
        }
        @Override
        public void onError(Throwable e) {
            Log.d(TAG,"In onError()");
            Log.d(TAG,""+e);
        }
        @Override
        public void onNext(List<ParentClass> parentClasses) {
            Log.d(TAG,"In onNext()");
            loadData(parentClasses);
        }
    });
android rx-java retrofit2 github-api rx-android
2个回答
1
投票

你得到的反应不是数组。 当使用json转换器时(在你的情况下它是gson),响应必须与POJO完全匹配。

你可以用itemClass这样的另一个类包装responseClass,它将具有:

class responseClass {
    int total_count;
    boolean incomplete_results;
    List<itemClass> items;
}

然后,在Client界面中:

@GET("repositories?q=topic:ruby+topic:rails")
Observable<responseClass> getAllUsers();      


Another option is not to use the auto converter, instead you may do:
@GET("repositories?q=topic:ruby+topic:rails")
Observable<JsonObject> getAllUsers();  //com.google.gson.JsonObject  

然后只使用gson转换items数组。

Type typeToken = new TypeToken<List<itemClass>>() {}.getType();
List<itemClass> items = new Gson().fromJson(response.get("items"), typeToken); 

2
投票

这个问题与RxJava无关。这是您的数据模型的问题:

Observable<List<itemsClass>> - 你期待实体列表,但实际上你的json是实体,有total_countincomplete_results和实际列表字段items(显然)itemsClass。请在Observable<...> getAllUsers();界面方法下使用正确的型号。

© www.soinside.com 2019 - 2024. All rights reserved.