Retrofit2 java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1行为STRING路径$

时间:2017-06-14 05:21:43

标签: android retrofit2

我使用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);

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

2 个答案:

答案 0 :(得分:1)

检查你的回答,它与你期望的不同。

Json必须从&#34; {&#34;响应也不同于你的Pojo 用户,所以从新的响应中创建新的Pojo。

答案 1 :(得分:1)

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

经过几个小时的调试,我发现这是由于我为请求设置了Accept-Encoding: gzip标头。实际上,这告诉okhttp 不要解压缩响应,并且调用者将自己完成响应。由于这个原因,gson试图反序列化未压缩的响应,这也就不足为奇了。