【问题标题】:Unable to get json string as response in Retrofit 2.0 android无法在 Retrofit 2.0 android 中获取 json 字符串作为响应
【发布时间】:2016-12-27 12:00:17
【问题描述】:

这不是重复的问题。 我将 Retrofit 2.0 和 json 用于网络任务。 此外,我没有使用 GSON 来解析 json,而是使用简单的 JsonObject 和 JsonArray 从 json 字符串中获取模型对象。 首先指导我在上述情况下必须使用哪种改装转换器。

其次,我无法将 json 字符串作为响应字符串。

我尝试了两种方法 - 方法 1 - 我使用了 Call。在这种情况下,response.body() 返回 null,尽管状态码是 200。

方法 2 - 我使用了 Call。在这种情况下, call.enqueue 方法调用“on failure method”而不是“onSuccess”,并且响应正文为空。 在这种情况下,状态码也是 200。

请建议如何获取 json 字符串作为改造 2.0 的响应。

【问题讨论】:

    标签: android retrofit retrofit2


    【解决方案1】:

    您需要使用JsonObject 而不是VoidResponseBody。你的代码应该是

    Call<JsonObject> getCall = request.getDataCall();
    getCall.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
    
        }
    
        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
    
        }
    });
    

    注意:确保您使用的是com.google.gson.JsonObject

    【讨论】:

    • 谢谢。它的工作。你能告诉我什么时候使用 void 和 response body。
    • this 可以让你清楚地了解它。
    • 好的,我会读的。如果可能的话,提供更多帮助....帮助我处理这篇文章stackoverflow.com/questions/38877535/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2016-08-28
    • 1970-01-01
    • 2019-10-05
    • 2017-10-28
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多