问题描述
我尝试添加一个带有Retrofit的用户,用户在Database中正确创建,但是我得到了以下错误:
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字节body)
03-14 06:04:27.732 30572-30600 / com.lehuo.lehuoandroid E / My Jobs:执行作业时出错
com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient (true)在第1行第1行接受格式错误的JSON com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1573)
在com.google.gson.stream处$
。 JsonReader.checkLenient(JsonReader.java:1423)
,位于com.google.gson.stream.JsonReader.doPeek( JsonReader.java:587)
在com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
在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)
从服务器返回的结果是:
{data:{user_id: 48,nickname:null,password:null,status:null},status:1,msg:OK}
这是正确的json格式,我不明白为什么我会得到这样的异常?
界面:
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用户用户);
}
public class User {
public int user_id;
公共字符串注册;
public int registration_type;
public String avatar;
public String昵称;
公共字符串密码;
公共字符串状态;
$ b $ public class Api {
// TODO修改值
public static final String BASE_URL =xxx;
private static Api instance = new Api();
public static Api getInstance(){
return instance;
$ b $ private Api(){}
public Retrofit getRetrofit(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(拦截器)
.retryOnConnectionFailure(true)
.connectTimeout(15,TimeUnit.SECONDS)
.build ();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(客户端)
.addConverterFactory(GsonConverterFactory.create())
.build();
返回改造;
}
public< S> S createService(Class< S> serviceClass){
return getRetrofit()。create(serviceClass);
调用者代码是:
ApiUsers api = Api.getInstance()。createService(ApiUsers.class);
呼叫< ApiResult<用户>> call = api.createUser(user);
CreateUserMessage message = new CreateUserMessage();
callNet(call,message);
任何人都可以提供任何线索吗?
最后我解决了与json lenient模式无关的问题,我的POST响应出现问题(在json数据之前有一些其他非json输出)。
$ b
以下是JakeWharton关于如何设置Gson宽松模式的回复:
确保你有:
compile'com.google.code.gson:gson:2.6.1'
Gson gson = new GsonBuilder()
.setLenient()
。创建();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(客户端)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
I am using retrofit:2.0.0-beta4 for my android app.
I tried to add a user with Retrofit, the user is correctly created in Database, however I got the following error:
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?
Finally I solved my problem which is not related to the json lenient mode, something wrong with my POST response (there some other non json output before the json data).
Here is the response from JakeWharton regarding how to set Gson lenient mode:
make sure that you have:
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();
这篇关于Retrofit2.0在json看起来正确的时候会得到MalformedJsonException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!