【问题标题】:java.lang.IllegalStateException: Expected BEGIN_OBJECTjava.lang.IllegalStateException:预期的 BEGIN_OBJECT
【发布时间】:2019-02-27 09:25:41
【问题描述】:

数据库上的数据发布。它给出了 Result Json 格式。

{"response":"exist"}

当使用 Retrofit 库获取响应并使用 GSON 解析 json 时,它会继续 OnFailure 方法并给出错误。

call.enqueue(new Callback<Register>() {
           @Override
           public void onResponse(Call<Register> call, Response<Register> response) {


               if (response.body().equals("ok")){

                   Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();

               }else if (response.body().equals("exiest")){

                   Toast.makeText(MainActivity.this, "Already Exiest", Toast.LENGTH_SHORT).show();

               }else if(response.body().equals("error")){

                   Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();

               }
           }

           @Override
           public void onFailure(Call<Register> call, Throwable t) {

               Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();

           }
       });

错误是:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

它是如何解决的,请指导我。

【问题讨论】:

  • 您是否已将 addConverterFactory(GsonConverterFactory.create()) 添加到改造构建器中
  • 在此处发布您的注册模型
  • Gson gson = new GsonBuilder() .setLenient() .create(); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build();
  • @POST("Registration.php") 调用 registerUser(@Field("fname") String fName, @Field("mname") String mName, @Field("lname") String lName, @Field("mobile_no") String mobile, @Field("email_id") String email);
  • @SerializedName("response") @Expose public String response; public String getResponse() { 返回响应; } public void setResponse(String response) { this.response = response; }

标签: android json gson


【解决方案1】:

您正在解析您的响应错误。和它的 exist ,你正在比较 exiest

像这样改变你的解析

if (response.body().getResponse().equals("ok")){

                   Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();

               }else if (response.body()..getResponse().equals("exist")){

                   Toast.makeText(MainActivity.this, "Already Exiest", Toast.LENGTH_SHORT).show();

               }else if(response.body().getResponse().equals("error")){

                   Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();

               }

【讨论】:

    【解决方案2】:

    您解析数据错误。在回调中给出的注册模型类中改造转换响应,您应该从中读取值。

    Register register= response.body();
    if (register.getResponse().equals("ok")){
       Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();
     }else if (register.getResponse().equals("exist")){
       Toast.makeText(MainActivity.this, "Already Exist", Toast.LENGTH_SHORT).show();
     }else if(register.getResponse().equals("error")){
        Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();
     }
    

    Register.java

    public class Register {
        @SerializedName("response")
        @Expose
        private String response;
        public String getResponse() {
            return response;
        }
        public void setResponse(String response) {
            this.response = response;
        }
    }
    

    【讨论】:

    • 您确定在响应中收到正确的 JSON 输出吗?
    • {"response":"exist"}
    • 我收到了上面的 JSON 输出。
    • 如果您收到上述输出,则不会出现此错误。检查这个答案stackoverflow.com/a/31425418/1546759
    猜你喜欢
    • 2014-08-02
    • 2020-02-10
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2017-04-16
    • 2018-12-14
    相关资源
    最近更新 更多