0

i'm using retrofit for my project. when i try to access my webservice,in onFailure i'm getting JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ on LogCat.i can get response from POSTMAN

this is postman response

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

OnCreate Update

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();

Method

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 service response

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

UpdateOrderResponse Module class

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;
    }
}

Update

i have added gson.setLenient() to retrofit.like below

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

then got another error on onFailure it is Expected BEGIN_OBJECT but was STRING at line 1. i have also added HttpLoggingInterceptor and checked my LogCat.it showing me

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 $
user8164155
  • 127
  • 1
  • 19
  • possible duplicate of [jsonReader.setLinent(true) error retrofit](https://stackoverflow.com/questions/39918814/use-jsonreader-setlenienttrue-to-accept-malformed-json-at-line-1-column-1-path).. check the solution given here – N.Moudgil Jan 16 '18 at 08:46
  • i have followed that answer.then i got `java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1`.now i have no idea how to convert my String response to Object – user8164155 Jan 16 '18 at 08:49
  • please go through the whole answer which include the answer for your next error..Add HttpLoggingInterceptor to log the response you are getting from server and then compare with your model – N.Moudgil Jan 16 '18 at 08:54
  • i have added `HttpLoggingInterceptor`.but nothing showing in LogCat.also i have updated my post – user8164155 Jan 16 '18 at 09:36
  • Paste your UpdateOrderResponse model, it seems you are returning array from php response ..correct me if I'm wrong bcz I don't know php – N.Moudgil Jan 16 '18 at 10:02
  • Php webservice response is object.it's response is top on my question (POSTMAN response ). – user8164155 Jan 16 '18 at 10:08
  • then please add a break point and debug the response – N.Moudgil Jan 16 '18 at 10:20
  • I have updated my post – user8164155 Jan 16 '18 at 10:53
  • Even though I don't have any idea of php..i would recommend altering your response..instead of returnjson try using json_encode refer : https://www.w3schools.com/js/js_json_php.asp – N.Moudgil Jan 16 '18 at 11:13

1 Answers1

0

As you are using GSON to parse your response, we need to use @SerializedName and @Expose tags in the model class. Please replace your UpdateOrderResponse with below code:

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;
    }

}
Roja Vangapalli
  • 259
  • 2
  • 5
  • I have replace my model class with your code.but same error `java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $` – user8164155 Jan 16 '18 at 11:02
  • If the keys and variable names are same then no need to explicitly mention SerializedName. – N.Moudgil Jan 16 '18 at 11:14