使用JsonReader.setLenient(true)在第1行第1列--Retrofit接受格式错误的JSON

时间:2018-01-16 08:42:42

标签: php android json retrofit retrofit2

我正在为我的项目使用改造。当我尝试访问我的网络服务时,在onFailure中,我在LogCat.i上获得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()进行改造。如下所示

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 $

1 个答案:

答案 0 :(得分:0)

当您使用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;
    }

}