【问题标题】:Rxjava, Retrofit response body doesn't get the right answer?Rxjava,Retrofit 响应正文没有得到正确答案?
【发布时间】:2017-03-06 19:24:08
【问题描述】:

我的代码是这样的:

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();


    this.client = new OkHttpClient.Builder().addInterceptor(logging)
            .connectTimeout(25, TimeUnit.SECONDS)
            .readTimeout(25, TimeUnit.SECONDS)
            .build();

    // retrofit with custom client


    this.retrofit = new Retrofit.Builder()
            .baseUrl(NetUtil.getServerBaseUrl())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(client)
            .build();
    this.apiService = retrofit.create(ApiEndpoints.class);
}



public Call<String> downloadImageCall(String uri){
    return apiService.rxGetImageCall(uri);
}

改造是这样的:

 @GET
 Call<String> rxGetImageCall(@Url String imageUrl);

当我运行我的代码时: 字符串 url="";

   Call<String> downCall =     downloadImageCall(url);
                    try {
                        Response<String> mresponse =  downCall.execute();
                        String info  =mresponse.body();
                        LogHelper.i("final info: "+info);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

从服务器返回的数据是这样的:

联系管理员”。

但是在上面的代码中,信息只是“Please

我对此感到困惑,我的代码哪里出错了?

【问题讨论】:

  • HttpLoggingInterceptor 拦截什么?附带说明:为什么要使用 RxJava 的调用适配器,但不使用 Observables 作为返回类型而不是 Call?
  • HttpLoggingInterceptor,我只想打印日志(请求和响应)。我不想使用 Observable 的原因,因为结果不是那么重要,即使它返回错误,对我来说也可以。所以我只是使用调用,但我的问题是:目前我只能得到部分结果。
  • 那么,body-interception(log)的结果和mresponse.body()一样吗?很难回答,因为我无法检查您的服务器响应。
  • mresponse.body的body-interception结果不同。你可以试试当url为“javascript.info/tutorial/hello-world”时。
  • 请查看:gist.github.com/anonymous/80f38b31a1bc15a71850c61fddd23b23。我不明白你为什么要使用改造或你想要达到的目标。

标签: retrofit rx-java okhttp3


【解决方案1】:

取决于@Hans 的回答,我将其更改为 Responsebody,它可以工作。但我不知道为什么"Call &lt;String&gt;" 不起作用。随时发布您的答案。

这是我的代码:

  @GET
 Call<ResponseBody> rxGetImageCall(@Url String imageUrl);


 public Call<ResponseBody> downloadImageCall(String uri){
  return apiService.rxGetImageCall(uri);
  }

 Call<ResponseBody> mbody = mOperation.downloadContentDetail("http://javascript.info/tutorial/hello-world");

                        try {
                            Response<ResponseBody> mResponse =  mbody.execute();
                            InputStream mStream =mResponse.body().byteStream();
                            StringWriter writer = new StringWriter();
                            BufferedInputStream bis = new BufferedInputStream(mStream);
                            ByteArrayOutputStream buf = new ByteArrayOutputStream();
                            int result = bis.read();
                            while(result != -1) {
                                buf.write((byte) result);
                                result = bis.read();
                            }
                            LogHelper.i("result 1" + buf.toString());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多