【发布时间】: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。我不明白你为什么要使用改造或你想要达到的目标。