我的Android应用程序使用的是RETERFIT:2.0.0-Beta4.

我试图添加一个改型用户,该用户已在数据库中正确创建,但出现以下错误:

03-14 06:04:27.731 30572-30600/com.lehuo.lehuoandroid D/OkHttp: CALLING POST SP_User_CreateUser....your new user_id:48
{"data":{"user_id":"48","nickname":null,"password":null,"status":null},"status":1,"msg":"OK"}
03-14 06:04:27.731 30572-30600/com.lehuo.lehuoandroid D/OkHttp: <-- END HTTP (147-byte body)
03-14 06:04:27.732 30572-30600/com.lehuo.lehuoandroid E/My Jobs: error while executing job
     com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
         at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1573)
         at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1423)
         at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:587)
         at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:202)
         at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:260)
         at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:32)
         at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:23)
         at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:213)
         at retrofit2.OkHttpCall.execute(OkHttpCall.java:177)
         at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:87)
         at com.lehuo.lehuoandroid.async.NetworkJob.callNet(NetworkJob.java:30)
         at com.lehuo.lehuoandroid.async.CreateUserJob.onRun(CreateUserJob.java:34)
         at com.path.android.jobqueue.BaseJob.safeRun(BaseJob.java:108)
         at com.path.android.jobqueue.JobHolder.safeRun(JobHolder.java:60)
         at com.path.android.jobqueue.executor.JobConsumerExecutor$JobConsumer.run(JobConsumerExecutor.java:201)
         at java.lang.Thread.run(Thread.java:818)

The returned result from server is :

{"data":{"user_id":"48","nickname":null,"password":null,"status":null},"status":1,"msg":"OK"}

This is correct json format, I don't understand why I get such exception?

Here us my interface:

public class ApiResult<T> {
    public T data;
    public int status;
    public String msg;
}

public interface ApiUsers {
    @POST("/users/new")
    public Call<ApiResult<User>> createUser(@Body User user);
}

public class User {
    public int user_id;
    public String registration;
    public int registration_type;
    public String avatar;
    public String nickname;
    public String password;
    public String status;

}

public class Api {

    // TODO modify the value
    public static final String BASE_URL = "xxx";

    private static Api instance = new Api();

    public static Api getInstance() {
        return instance;
    }

    private Api(){}

    public Retrofit getRetrofit() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .retryOnConnectionFailure(true)
                .connectTimeout(15, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit;
    }

    public <S> S createService(Class<S> serviceClass) {
        return getRetrofit().create(serviceClass);
    }
}

The caller code is:

ApiUsers api = Api.getInstance().createService(ApiUsers.class);
Call<ApiResult<User>> call = api.createUser(user);
CreateUserMessage message = new CreateUserMessage();
callNet(call, message);

Could anyone give any clue?

推荐答案

最后,我解决了与json宽松模式无关的问题,即我的POST响应有问题(json数据之前还有其他一些非json输出).

Here is the response from JakeWharton regarding how to set Gson lenient mode:

确保你有:compile 'com.google.code.gson:gson:2.6.1'

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

Json相关问答推荐

JOLT将对象名作为新属性添加到主体中

将JSON输入子数组转换为字符串顺序列表输出

如何循环访问多个子数组并在单个JSON中检索数据点

使用更高级别架构中的字段值在$def内实现约束

Jolt-在数组列表中插入新的字段

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

使用jq根据对象中键的值查找对象

如何通过 jolt 将一个对象中的键和值添加到数组中的每个对象中

添加到数组时出错:找不到Add的重载和参数计数:1

JQuery,使用 GET 方法发送 JSON 对象

如何使用 CORS 实现 JavaScript Google Places API 请求

Java的JSON字符串整洁/格式化程序

IE8 原生 JSON.parse 错误导致堆栈溢出

Jackson Scala 模块的小例子?

ASP.NET MVC:使用 JsonResult 控制属性名称的序列化

字符串的 Gson 数组到 JsonArray

jQuery.getJSON 和 jQuery.parseJSON 返回 [object Object]?

Java循环遍历Json数组?

如何使用 Javascript 将数据写入 JSON 文件

Java HashMap 与 JSONObject