使用带有Rxjava2的翻新2发送GET请求,获取空对象

时间:2019-01-21 09:00:40

标签: android retrofit2

  

我知道以前有几个人问过这个问题,但是我已经尝试了这些答案。我是Android新手。一切看起来不错,但是我不明白为什么我得到了一个空物体?有人可以指导我吗?

接口:

public interface CryptoAPI {
@GET("ActivityForTestingPurposeOnly/")
io.reactivex.Observable<List<Stream>> getData(@Query("Row") String Row,
                                             @Query("Top") String Top,
                                             @Query("AppID") String appid);

这是我的改装适配器:

public class RetrofitAdapter {
   private String BASE_URL = "http://m.ilmkidunya.com/api/sectionactivity/sectionactivity.asmx/"

   public static CryptoAPI newAPICreator() {
       final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
       interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
       final OkHttpClient client = new OkHttpClient.Builder()
                                    .addInterceptor(interceptor)
                                    .build();

       Retrofit retrofit = new Retrofit.Builder()
             .client(client)            
             .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
             .addConverterFactory(GsonConverterFactory.create())
             .baseUrl(BASE_URL)
             .build();

            return retrofit.create(CryptoAPI.class);
        }
    }
}

最后,我得到响应的方法:

public void getStreams(){
  CryptoAPI.RetrofitAdapter.newAPICreator()
    .getData("0", "20", "73")
    .subscribeOn(Schedulers.io())
    .subscribeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<Stream>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Stream model) {
                arrayList.add(model);                                  
                Toast.makeText(getApplicationContext(),
                               "Size: " + arrayList.size(),
                               Toast.LENGTH_SHORT);

        }
        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onComplete() {

        }
    });
}

> here is my 



    @SerializedName("ID")
    @Expose
    private Integer iD;
    @SerializedName("Rating")
    @Expose
    private Integer rating;
    @SerializedName("SectionID")
    @Expose
    private Integer sectionID;
    @SerializedName("ContentID")
    @Expose
    private Integer contentID;

here you can see in the image

1 个答案:

答案 0 :(得分:1)

摘要

我测试了您给定的端点,并且工作正常

curl -X GET \
  'http://m.ilmkidunya.com/api/sectionactivity/sectionactivity.asmx/ActivityForTestingPurposeOnly?Row=0&Top=20&AppId=73' \
  -H 'Postman-Token: f64fdf34-4dbb-4201-93e9-e4c95fe7064d' \
  -H 'cache-control: no-cache'

返回是

{
    "stream": [
        {
            "ID": 583750,
            "Rating": 0,
            "SectionID": 59,
            "ContentID": 0,
            "SectionName": "Comments",
            "SortOrder": 2,
            "Title": "ICS",
            ...
        }
    ]
}

出了什么问题

您使用过Observable<List<DashboardDs>>List<T>类表示您期望数组作为您的ROOT json响应

您需要做什么

  

已更新(基于用户的问题更新)

像这样创建一个名为Stream的对象

public class Stream {
     @SerializedName("stream")
     List<StreamItem> streamItems; // <-- Array list of the stream items you have
}

像这样创建一个名为StreamItem的对象

public class StreamItem {
     @SerializedName("ID") // <- json key
     Int id;
     @SerializedName("Rating")
     Int rating;
     @SerializedName("SectionID") 
     Int sectionId;
     @SerializedName("ContentID")
     Int contentId;
     @SerializedName("SectionName")
     String sectionName;
     @SerializedName("Title")
     String title;
     ... // additional properties you need
}

然后像这样更改您的api服务界面

io.reactivex.Observable<Stream>

此外,如果您不将rxjava2与旧的rxjava1或任何相关的Observable类一起使用,则可以直接将Observable类导入服务类的最顶部 >

import io.reactivex.Observable

并像这样使用它

Observable<Stream>

通过这种方式,使用我上面提供的Stream模型对象

public interface CryptoAPI {
@GET("ActivityForTestingPurposeOnly/")
Observable<Stream> getData(@Query("Row") String Row,
                                             @Query("Top") String Top,
                                             @Query("AppID") String appid);

这就是你的称呼方式

public void getStreams(){
  CryptoAPI.RetrofitAdapter.newAPICreator()
    .getData("0", "20", "73")
    .subscribeOn(Schedulers.io())
    .subscribeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<Stream>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Stream stream) {
               // IMPORTANT NOTE:
               // Items return here in onNext does not mean the 
               // object you have (eg. each Stream's object in streams' list) 
               // it represents streams of generic data objects


               // This is where you can access the streams array
               arrayList.addAll(stream.streamItems) // <- notice the usage

               Toast.makeText(getApplicationContext(),
                               "Size: " + arrayList.size(),
                               Toast.LENGTH_SHORT);

        }
        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onComplete() {

        }
    });
}

了解更多信息