【问题标题】:MalformedJsonException on Retrofit and Gson改造和 Gson 上的 MalformedJsonException
【发布时间】:2017-12-10 00:21:18
【问题描述】:

我正在尝试进行改造调用,但我遇到了 MalformedJsonException。

这是我的电话:

String idFoto = "1ead0a1f-bbc4-46bd-901c-7988c0e6c68b";
Retrofit retrofit = new Retrofit.Builder()
     .baseUrl(Global.URL_BASE)
     .addConverterFactory(GsonConverterFactory.create())
     .build();
FotosAPI service = retrofit.create(FotosAPI.class);
Call<String> obtenerFotoCall = service.getFoto(Global.getToken(), idFoto);

这是我的界面:

public interface FotosAPI {
     @GET(Global.URL_FOTO + "{id}")
     Call<String> getFoto(@Header("Authorization") String token, @Path("id") String id);
}

调用enqueue进入onFailure方法,报错“com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2 path $”。

我进行了更改以设置 lenient,如下所示:

Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder()
     .baseUrl(Global.URL_BASE)
     .addConverterFactory(GsonConverterFactory.create(gson))
     .build();

然后,我有一个不同的错误:“com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $”

我认为错误可能是在 idFoto 字符串值的 Gson 转换中,但我不知道是什么问题。

有人可以帮帮我吗?

谢谢你们!

【问题讨论】:

  • 你应该需要ToStringConverterFactory,因为我认为你的回复是String所以不需要转换成GsonConverterFactory
  • 你能看到原始 json 响应的样子吗?
  • 原始调用如下所示:Request{method=GET, url=10.0.2.2:8080/DGCWS/rest/photo/…, tag=null}。调用结果进入onFailure方法时不知道在哪里可以看到原始json响应
  • 我有其他 post call 有一个字符串响应(获取令牌)并且使用 gson 和 lenient 我没有问题

标签: android gson retrofit


【解决方案1】:

请检查您从服务器获取的 JSON,在我的情况下,我从服务器获取的 JSON 无效。

尝试从 Postman 或其他工具检查您的 JSON。

【讨论】:

  • 嗨希瓦姆。感谢您的答复。我这样做了,我得到了正确答案(base64 中的照片)。
【解决方案2】:

就像 M D 说的(非常感谢你!),解决方案是将 GsonConverter 更改为 ToStringCorverterFactory。

这是我使用的类:

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class ToStringConverterFactory extends Converter.Factory {
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        if (String.class.equals(type)) {
            return new Converter<ResponseBody, String>() {
                @Override
                public String convert(ResponseBody value) throws IOException {
                    return value.string();
                }
            };
        }
        return null;
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
                                                          Annotation[] methodAnnotations, Retrofit retrofit) {

        if (String.class.equals(type)) {
            return new Converter<String, RequestBody>() {
                @Override
                public RequestBody convert(String value) throws IOException {
                    return RequestBody.create(MEDIA_TYPE, value);
                }
            };
        }
        return null;
    }
}

然后您只需将改造构建器更改为:

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Global.URL_BASE)
                        .addConverterFactory(new ToStringConverterFactory())
                        .build();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2012-07-14
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    相关资源
    最近更新 更多