【问题标题】:Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 -Retrofit使用 JsonReader.setLenient(true) 在第 1 行第 1 列接受格式错误的 JSON -Retrofit
【发布时间】:2018-06-24 22:02:30
【问题描述】:

我正在为我的项目使用改造。当我尝试访问我的网络服务时,在 onFailure 中我在 LogCat 上收到 JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $。我可以从 POSTMAN 获得响应

这是邮递员的回复

{
    "status": true,
    "message": "order status updated"
} 

OnCreate 更新

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        retrofit = new Retrofit.Builder()
                            .baseUrl("https://example.com/sample/")                    
                            .client(client)
    .addConverterFactory(GsonConverterFactory.create())
                            .build();
        token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9";
        id_order= 288;
        status="process";
        update();

方法

public void update(){
        WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
        Call<UpdateOrderResponse> call = apiService.updateOrder("pickorder",token,id_order,status);
        call.enqueue(new Callback<UpdateOrderResponse>() {
            @Override
            public void onResponse(Call<UpdateOrderResponse> call, Response<UpdateOrderResponse> response) {

                UpdateOrderResponse result = response.body();
                Log.d("orderstatus", "body: "+result);
                returnstatus=result.isStatus();
                msg= result.getMessage();

                if(returnstatus){
                    Log.d("orderstatus","status ok");
                }else{
                    Log.d("orderstatus","status not ok");
                }
            }

            @Override
            public void onFailure(Call<UpdateOrderResponse> call, Throwable t) {
                Log.d("proceedFail",""+t.getMessage());

            }
        });

    }

PHP 服务响应

$response = ['status' => true,
        'message' => "order status updated",
         ];
$this->returnJson($response);

UpdateOrderResponse 模块类

public class UpdateOrderResponse {
    boolean status;
    String message;

    public boolean isStatus() {
        return status;
    }    
    public void setStatus(boolean status) {
        this.status = status;
    }    
    public String getMessage() {
        return message;
    }    
    public void setMessage(String message) {
        this.message = message;
    }
}

更新

我已将gson.setLenient() 添加到retrofit.like 下面

Gson gson = new GsonBuilder()
                .setLenient()
                .create();
retrofit = new Retrofit.Builder()
                .baseUrl("https://example.com/sample/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

然后在onFailure 上出现另一个错误,它是Expected BEGIN_OBJECT but was STRING at line 1。我还添加了HttpLoggingInterceptor 并检查了我的 LogCat.it 显示我

OkHttp: <-- 200 OK https://........
OkHttp: {"status":true,"message":"order status updated"}
D/OkHttp: <-- END HTTP (99-byte body)    
proceedFail: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

【问题讨论】:

  • jsonReader.setLinent(true) error retrofit.. 的可能重复项检查此处给出的解决方案
  • 我已经遵循了那个答案。然后我得到了java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1。现在我不知道如何将我的字符串响应转换为对象
  • 请仔细阅读整个答案,其中包括下一个错误的答案。添加 HttpLoggingInterceptor 以记录您从服务器获得的响应,然后与您的模型进行比较
  • 我已经添加了HttpLoggingInterceptor。但是 LogCat 中没有显示任何内容。我也更新了我的帖子
  • 粘贴您的 UpdateOrderResponse 模型,看来您正在从 php 响应返回数组。如果我错了,请纠正我,因为我不知道 php

标签: php android json retrofit retrofit2


【解决方案1】:

当您使用 GSON 解析您的响应时,我们需要在模型类中使用 @SerializedName 和 @Expose 标签。 请将您的 UpdateOrderResponse 替换为以下代码:

public class UpdateOrderResponse {

    @SerializedName("status")
    @Expose
    private Boolean status;

    @SerializedName("message")
    @Expose
    private String message;

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

【讨论】:

  • 我已经用你的代码替换了我的模型类。但是同样的错误java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
  • 如果键名和变量名相同,则无需显式提及 SerializedName。
猜你喜欢
  • 2017-02-16
  • 2017-06-03
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
  • 2018-05-17
  • 1970-01-01
相关资源
最近更新 更多