Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当json看起来正确时,Reformation2.0会得到格式错误的JSONException?_Android_Json_Gson_Retrofit_Retrofit2 - Fatal编程技术网

Android 当json看起来正确时,Reformation2.0会得到格式错误的JSONException?

Android 当json看起来正确时,Reformation2.0会得到格式错误的JSONException?,android,json,gson,retrofit,retrofit2,Android,Json,Gson,Retrofit,Retrofit2,我正在为我的android应用程序使用改型: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,"statu

我正在为我的android应用程序使用改型: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)
这是正确的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 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);
    }
}
公共类ApiResult{
公共数据;
公众地位;
公共字符串msg;
}
公共接口用户{
@帖子(“/users/new”)
公共调用createUser(@Body User);
}
公共类用户{
公共int用户id;
公共字符串注册;
公共int注册类型;
公共字符串化身;
公共字符串昵称;
公共字符串密码;
公共字符串状态;
}
公共类Api{
//TODO修改该值
公共静态最终字符串BASE_URL=“xxx”;
私有静态Api实例=新Api();
公共静态Api getInstance(){
返回实例;
}
私有Api(){}
公共改装GetRefundation(){
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder()
.addInterceptor(拦截器)
.retryOnConnectionFailure(真)
.connectTimeout(15,时间单位为秒)
.build();
改装改装=新改装.Builder()
.baseUrl(基本URL)
.客户(客户)
.addConverterFactory(GsonConverterFactory.create())
.build();
回流改造;
}
公共的createService(类serviceClass){
返回getReformation().create(serviceClass);
}
}
呼叫方代码为:

ApiUsers api = Api.getInstance().createService(ApiUsers.class);
Call<ApiResult<User>> call = api.createUser(user);
CreateUserMessage message = new CreateUserMessage();
callNet(call, message);
ApiUsers-api=api.getInstance().createService(ApiUsers.class);
Call Call=api.createUser(用户);
CreateUserMessage=新建CreateUserMessage();
callNet(呼叫、消息);

有人能提供一些线索吗?

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

以下是JakeWharton关于如何设置Gson宽大模式的回应:

确保您有:
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();
我解决了这个问题

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

OkHttpClient client = new OkHttpClient();

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://kafe.netai.net/")
    .client(client)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build();



compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.7'

我通过从JSON文件中删除额外的HTML文本和其他空格来解决问题。

一些字符,如NUL
\0
也可能导致此问题,我们在调试中无法看到此字符。尝试使用宽松模式。同时在初始化Retrofit的地方添加代码,即
Api.getInstance()
User
类。您应该看到@vedant1811,不幸的是,我没有找到编写自定义gson转换器的教程,请检查这里,它尚未发布。用户id从webservice获取字符串,并将您的接口转换为整数。请检查它。不工作,因为我在XML数据中的响应,我尝试了所有可能的方法。在实现此解决方案后,我仍然得到相同的错误。你能给我一些指示来修正这个错误吗?我使用的是改型2,处理的是JSON而不是XML,虽然我没有尝试过,但可以帮助别人。试试看。@TotZam哈哈哈!这里的代码是未格式化的!:D:DHmm,所以我也会试着把我的代码写在一行中——也许这会解决问题。)请告诉我怎么做?@roghayehhosseini我的JSON文件中几乎没有多余的空间,这就是为什么我收到一个格式错误的JSON异常。然后我删除了所有多余的空间。
Gson gson = new GsonBuilder().setLenient().create();

OkHttpClient client = new OkHttpClient();

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://kafe.netai.net/")
    .client(client)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build();



compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.7'