【问题标题】:Retrofit2 java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $Retrofit2 java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 1 列路径 $
【发布时间】:2017-06-14 05:21:43
【问题描述】:

我必须使用 retrofit2 创建/注册新用户,我的服务器端是

<?php

    $name= $_POST['name'];
    $email = $_POST['email'];
    $password= $_POST['password'];
    $gender= $_POST['gender'];

    $con = mysqli_connect("localhost", "root", "qwerty", "db");
    $query= mysqli_query($con, "INSERT INTO users(name,email, password, gender) VALUES('$name','$email', '$password', '$gender')");

    if($query){
        echo "You are sucessfully Registered";
    }

    else{
        echo "your details could not be registered";
    }

    mysqli_close($con);

?>

我的模型类是

public class User {

    @SerializedName("name")
    @Expose
    public String name;
    @SerializedName("email")
    @Expose
    public String email;
    @SerializedName("password")
    @Expose
    public String password;
    @SerializedName("gender")
    @Expose
    public String gender;

    public User(String name, String email, String password, String gender) {
        this.name = name;
        this.email = email;
        this.password = password;
        this.gender = gender;
    }

服务中的POST是

@POST("register.php")
Call<User> createUser(@Body User user);

将数据发布为

mRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (validateForm()){
                    mUserCall = mRestManager.getJobService()
                            .createUser(new User(mNameEditText.getText().toString(),
                                                mEmailEditText.getText().toString(),
                                                mPasswordEditText.getText().toString(),
                                                mGenders[position]));
                    mUserCall.enqueue(new Callback<User>() {
                        @Override
                        public void onResponse(Call<User> call, Response<User> response) {
                            User user1 = response.body();
                            Toast.makeText(getApplicationContext(), user1.name , Toast.LENGTH_SHORT).show();
                        }

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

                            Log.e("REGISTER_ERROR", "Message is " + t.getMessage() );
                        }
                    });
                }
            }
        });

我遇到了错误

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

我找到了一些解决方案并相应地进行了更改

@FormUrlEncoded
    @POST("register.php")
    Call<User> createUser(@Field("name") String name,
                          @Field("email") String email,
                          @Field("password") String password,
                          @Field("gender") String gender);

但它也不起作用,同样的错误信息,如何解决这个错误。

【问题讨论】:

  • 使用调用
  • 添加您的 json 响应
  • 你没有返回一个用户对象,你在你的 PHP 中返回一个成功/失败的字符串。
  • @DivyeshPatel 如何从call&lt;ResponseBody&gt; 获取用户数据?
  • 当我更改 Call&lt;ResponseBody&gt; 并打印 Log.e("RESPONSE", "Is " + response.body().toString()); 时,我得到了 okhttp3.ResponseBody$1@38c108b7

标签: android retrofit2


【解决方案1】:

即使我知道服务器返回了有效的 json,我也收到了 Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 错误。

经过几个小时的调试,我发现这是由于我将 Accept-Encoding: gzip 标头设置为请求。这实际上告诉 okhttp 不要解压缩响应,调用者将自己完成。由于这个 gson 试图反序列化解压缩的响应,难怪这会失败。

【讨论】:

    【解决方案2】:

    检查您的回复,它与您的预期不同。

    Json 必须从“{”开始,响应也不同于您的 Pojo 用户,所以从新响应中创建新的 Pojo。

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 2017-05-12
      • 2017-07-04
      • 2020-11-09
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多