【发布时间】:2018-12-09 10:23:15
【问题描述】:
我想使用改造从我的服务器获取数据。我的服务器将数据作为字符串 json 发送。 我创建一个这样的服务器:
public class ServiceGenerator {
public static final String BASE_URL = "http://192.168.100.73/ChartReport/Service1.svc/";
static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create());
private static Retrofit retrofit = builder.build();
public static <S> S createService(Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
}
然后我创建了像打击一样的客户端:
public interface IReportCLient {
@POST("json/GetDataReport")
Call<ResponseBody> getReporst();
}
我已经习惯了我的活动:
IReportCLient service = ServiceGenerator.createService(IReportCLient.class);
Call<ResponseBody> reporst = service.getReporst();
reporst.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
JsonObject post = new JsonObject().get(response.body().string()).getAsJsonObject();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
当我第一次在调试模式下运行我的应用程序时,我通过以下命令获取我的数据:
response.body().string()
但是当我再次运行response.body().string() 时,我的结果立即为空?
发生了什么?
【问题讨论】:
-
发布回复
-
@ALTegani 是什么意思?你的意思是回答我的问题?
-
不是指服务器发送的响应
-
@ALTegani 响应是由服务器发送的。我相信 100%。你可以在图片中看到
GetDataReportResult