【问题标题】:Sending a GET request using retrofit 2 with Rxjava2, Getting Empty Object使用 Rxjava2 的改造 2 发送 GET 请求,获取空对象
【发布时间】:2019-06-14 15:10:12
【问题描述】:

我知道以前有人问过这个问题,但我已经尝试过这些答案。我是安卓新手。一切看起来都不错,但我不明白为什么我得到一个空对象?任何人都可以指导我吗?

界面:

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

【问题讨论】:

  • 你有没有试过在浏览器或邮递员上点击api来检查它是否真的是服务器的问题?
  • 500-Internal Server Error 表明此错误来自服务器端,而不是您的端。
  • 是的,我收到了准确的回复。
  • 解决了 URL 错误的问题,现在出现此错误。
  • 这听起来像你期待一个数组[ ... ],但得到一个对象{ ... }。检查您收到的 JSON 并相应地调整您的代码。

标签: android retrofit2


【解决方案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&lt;List&lt;DashboardDs&gt;&gt;List&lt;T&gt; 类表明您期望一个数组作为您的 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() {

        }
    });
}

了解更多

【讨论】:

    猜你喜欢
    • 2018-11-13
    • 1970-01-01
    • 2021-12-13
    • 2018-07-07
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    相关资源
    最近更新 更多