【问题标题】:How to fix 'Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $' error in Android Studio如何修复 Android Studio 中的“使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 处接受格式错误的 JSON”错误
【发布时间】:2020-02-08 17:18:37
【问题描述】:

我正在尝试将数据发布到 API,但出现此错误:

使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 处接受格式错误的 JSON

有人知道如何解决这个问题吗?

在活动中发布数据的代码

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

API 应该返回

{ “status”:”00”,
  ”data”:”List Data”}

【问题讨论】:

    标签: java android json android-studio


    【解决方案1】:

    您确定您的 json 响应格式不正确吗? 请在 https://jsonlint.com/ 之类的网站上验证您的回复,看看您的回复是否是正确的 json。

    【讨论】:

      【解决方案2】:

      模型 PostGetListKredit 是匹配 json,但您使用的响应是 PostGetList - 而不是 PostGetListKredit。 尝试:将 PostGetList 更改为 PostGetListKredit

      【讨论】:

      • 你应该改变对 JsonObject 的响应 - 为了确定响应的格式,我认为上面的 json 不正确。
      【解决方案3】:

      设置标题,

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

      并且还使用Gson进行json转换,

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

      如需更详细的答案,请关注link

      【讨论】:

      • 已阅读该线程,但需要更改 API 响应。对于 API 发送的响应 JSON,我无能为力。我无法改变它。
      猜你喜欢
      • 2017-06-03
      • 2017-02-16
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多