Rxjava,改造响应体没有得到正确的答案?



我的代码如下:

    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 contact the administrator".

然而在上面的代码中,信息只是"Please"

我很困惑,我的代码哪里错了?

取决于@Hans的答案,我将其更改为Responsebody,它有效。但我不知道为什么"Call <String>"不起作用。请随意发表你的答案。

这是我的代码:

  @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();
                        }

相关内容

  • 没有找到相关文章

最新更新