1

I've to create/register new User using retrofit2 my server side is

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

?>

my model class is

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 in service is

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

posting data as

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

I'm getting error

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

I've found some solutions and made change accordingly as

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

but it also does not work, same error message, how to solve this error.

17
  • use call<ResponseBody> Commented Jun 14, 2017 at 5:27
  • add your json response Commented Jun 14, 2017 at 5:28
  • you are not returning a User object you are returning a String for Success/Failure in your PHP. Commented Jun 14, 2017 at 5:45
  • @DivyeshPatel how can I get User data from call<ResponseBody>? Commented Jun 14, 2017 at 8:25
  • I'm getting okhttp3.ResponseBody$1@38c108b7 when I change Call<ResponseBody> and print Log.e("RESPONSE", "Is " + response.body().toString()); Commented Jun 14, 2017 at 8:29

2 Answers 2

2

I was also getting Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ error even though I knew server was returning valid json.

After some hours debugging this I discovered it was due to me setting Accept-Encoding: gzip header to the request. This actually tells okhttp not to unzip the response and that the caller will do it himself. Due to this gson was trying to deserialize unzipped response and it's no wonder this was failing.

Sign up to request clarification or add additional context in comments.

Comments

1

Check your response, it is different than you are expecting.

Json must have start from "{" also response is different than your Pojo User so make new Pojo from new response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.