【问题标题】:How to properly handle text/plain responses with retrofit?如何通过改造正确处理文本/简单响应?
【发布时间】:2015-12-14 06:06:00
【问题描述】:

我有一个 web 服务调用,它的响应是纯文本,没有 json。在下面的回调中,即使响应成功,也会调用 failure(),因为改造会尝试将响应解析为 json。

new Callback<String>() {
        @Override
        public void success(String s, Response response) {
        ...    
        }

        @Override
        public void failure(RetrofitError error) {
        ...
        }
    });

出现以下错误:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 1 行第 16 列路径 $ 接受格式错误的 JSON

我如何告诉改造,响应是纯文本,不应该被视为 json?

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    您正在使用 GsonConverter 进行反序列化,而 Gson 无法将响应验证为 Json 格式。这就是你得到 MalformedJsonException 的原因。您需要在 RestAdapter 中提供类似 StringConverter 的内容

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      相关资源
      最近更新 更多