Rxjava,Retrofit响应机构没有得到正确的答案?

时间:2016-10-24 21:59:44

标签: retrofit rx-java okhttp3

我的代码是这样的:

    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);

当我运行我的代码时:         String 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();
                    }

从服务器返回的数据如下:

与管理员联系”。

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

我对此感到困惑,我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

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