How to fix 'Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $' error in Android Studio

13,399

Are you sure your json response is not malformed? please verify your response in some site like https://jsonlint.com/ to see if your response is correct json or not.

Share:
13,399

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to POST data to API, but I get this error:

    Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

    Anyone knows how to fix this?

    Code to POST the data In Activity

    private void callList(){
        String signature = new String(Hex.encodeHex(DigestUtils.md5(
                TMK + appId + appVersion +
                        SessionManager.getSession(getApplicationContext(), getString(R.string.session_token)))
        ));
    
        Call<PostGetList> postGetListCall = mApiInterface.postGetList(
                appId,
                appVersion,
                SessionManager.getSession(getApplicationContext(), getString(R.string.session_token)),
                signature);
    
        postGetListCall.enqueue(new Callback<PostGetList>() {
            @Override
            public void onResponse(Call<PostGetList> call, Response<PostGetList> response) {
                status = response.body().getStatus();
                Utils.logcat("Recv getInfo Status " + status);
                if (status.equals(getString(R.string.rc00))) {
                    Message.message(getApplicationContext(), "Success");
                } else {
                    Message.message(getApplicationContext(), status);
                }
            }
    
            @Override
            public void onFailure(Call<PostGetList> call, Throwable t) {
                String responseMsg = "Fail to connect";
                Utils.logcat(responseMsg);
                Toast.makeText(getApplicationContext(), responseMsg, Toast.LENGTH_LONG).show();
                Utils.logcat(t.toString());
            }
        });
    }
    

    ApiInterface.java

    @FormUrlEncoded
    @POST("List.php")
    Call<PostGetList> postGetList(@Field("appId") String appId,
                                  @Field("appVersion") String appVersion,
                                  @Field("token") String token,
                                  @Field("signature") String signature);
    

    PostGetList.java

    public class PostGetList {
        @SerializedName("status")
        String status;
        @SerializedName("data")
        String data;
    
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
    
        public String getData() {
            return data;
        }
        public void setData(String data) {
            this.data = data;
        }
    }
    

    The API should return

    { “status”:”00”,
      ”data”:”List Data”}
    
  • TieuNhi
    TieuNhi over 4 years
    you should change response to JsonObject - to sure format of response, i think the the json above not right.
  • Admin
    Admin over 4 years
    Already read that thread, but need to change the API response. I can't do anything for the response JSON that API send. I can't change it.