2

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

3 Answers3

2

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.

DinkarKumar
  • 1,492
  • 8
  • 19
0

the model PostGetListKredit is match json, but response you use is PostGetList - not PostGetListKredit. try : change PostGetList to PostGetListKredit

TieuNhi
  • 35
  • 4
  • you should change response to JsonObject - to sure format of response, i think the the json above not right. – TieuNhi Oct 11 '19 at 10:26
0

Set header,

@Headers("Content-Type: application/json")

And also use Gson for json conversion,

make sure that you have:
   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();

For more detailed answer follow up this link

  • 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. –  Oct 16 '19 at 07:48