RAJESH KUMAR ARUMUGAM
RAJESH KUMAR ARUMUGAM

Reputation: 1570

Expected BEGIN_OBJECT but was STRING at line 1 - Retrofit 2

I am trying to get and Post Data using Retrofit But it Throws Exception Fsaailurecom.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ I tried all possible solutions suggested by the StackOverflow community but still the Problem Exits :( Any Help..!!

PS: In brwoser and Localhost i can able to get the Correct Json Response for the API call http://onesignaldemo.rf.gd/test/get_user.php . In postman it returns This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support

API CALL

private void getUsersFromDB() {
        userservice= ApiUtils.getUserservice();
        userservice.getUsers().enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                Log.d("RESULT", "onResponse: --------------------------------SUCCESS");
                List<Result> userresponses = response.body().getResult();
                Log.d("ANS", "Number of movies received: " + userresponses.size());

            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.d("RESULT", "onResponse: --------------------------------Failure" +t);

            }
        });


    }

UserModal.java

public class UserModel {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("user_name")
    @Expose
    private String userName;
    @SerializedName("user_password")
    @Expose
    private String userPassword;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

}

UserResponse.java

public class UserResponse {

    @SerializedName("result")
    @Expose
    private List<Result> result = null;
    @SerializedName("error_code")
    @Expose
    private Integer errorCode;

    public List<Result> getResult() {
        return result;
    }

    public void setResult(List<Result> result) {
        this.result = result;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }
}

Interface

public interface UserService {
    @GET("/get_user.php")
    Call<UserResponse> getUsers();

    @FormUrlEncoded
    @POST("/insert_user.php")
    Call<DBResponse> insertUsers(@Field("user_name") String username,
                                 @Field("user_password") String userpassword);
}

This is my Json

{"result":[{"Users":{"id":"1","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"2","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"3","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"4","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"5","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"6","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"7","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"8","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"9","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"10","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"11","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"12","user_name":"raj","user_password":"rajesh123"}}]}

Upvotes: 1

Views: 4463

Answers (2)

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

Server is returning just a String, not an object. Try to see what is the answer in Fiddler or just create a request in Postman with the same request data to see the answer.

As i can see, in result you have a list of objects, object encoded in User that return your UserModal.java. So, you need to create an intermediar object between result and your UserModal.java. Something like:

public IntermediarUser {
    @SerializedName("User")
    public UserModal user;

    // getter, setter, etc
}

If you don't want to do that, check your server implementation for cloud.

Upvotes: 1

Garry McKee
Garry McKee

Reputation: 141

Might be wrong but it looks like your JSON returns one result object which has a list of users rather than a list of results? So you should have a Result pojo with a List property then your user object, I think, should have three strings: id, user_name and user_password. That error nearly always (if not always) refers to an a mismatch in the structure of your pojos/data model and the structure of the JSON.

Upvotes: 0

Related Questions