【问题标题】:Result disappeared in retrofit 2.4.0结果在改造 2.4.0 中消失了
【发布时间】: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() 时,我的结果立即为空?

发生了什么?

【问题讨论】:

  • 发布回复
  • @ALTe​​gani 是什么意思?你的意思是回答我的问题?
  • 不是指服务器发送的响应
  • @ALTe​​gani 响应是由服务器发送的。我相信 100%。你可以在图片中看到 GetDataReportResult

标签: android retrofit


【解决方案1】:

string() 方法只能在 RequestBody 上调用一次。因此,如果您再次尝试调用它,它将返回空字符串。调试也是如此。如果您在调试时尝试评估表达式response.body().string(),您的实际方法将得到空字符串。

HTTP 响应。此类的实例不是不可变的: 响应体是一个一次性值,只能使用一次,并且 然后关闭。所有其他属性都是不可变的。 https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html

也请阅读此内容https://stackoverflow.com/a/32307866/6168272

这就是我从响应对象中获取JsonObject 的方式。你可以试试看。

private JSONObject parseJsonFromResponse(Response response) {
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                try {
                    return new JSONObject(responseBody.string());
                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                    return new JSONObject();
                }
            } else return new JSONObject();
        }

【讨论】:

  • 在任何情况下,如果我不使用调试模式,我在new JsonObject().get(response.body().string()).getAsJsonObject(); 上得到错误我得到java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.gson.JsonObject com.google.gson.JsonElement.getAsJsonObject()' on a null object reference
  • 这意味着你 JsonObject 为 null,因为 JsonObject().get(responseBody) 返回 null。
  • 当然,但有一段时间不是......你可以在我发送的图片中看到
  • response.body().string() 不为空,您的JsonObject().get() 方法无法解析或返回空对象。
  • 可以修改else部分。我需要一个空的JSONObject
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
相关资源
最近更新 更多