Rxjava,Retrofit 响应体没有得到正确答案?
Rxjava, Retrofit response body doesn't get the right answer?
我的代码是这样的:
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”
我很困惑,我的代码哪里错了?
看@Hans的回答,我改成了Responsebody,可以。但是我不知道为什么 "Call <String>"
不起作用。欢迎post回答。
这是我的代码:
@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();
}
我的代码是这样的:
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”
我很困惑,我的代码哪里错了?
看@Hans的回答,我改成了Responsebody,可以。但是我不知道为什么 "Call <String>"
不起作用。欢迎post回答。
这是我的代码:
@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();
}