Attempt to read from field error in retrofit 2.1.0 when data fetch in onRresponse method through Model class

  • Replies:0
Bhavesh Patel
  • Forum posts: 2

Mar 2, 2017, 8:38:50 AM via Website

===>>>my interface

interface loginInterFace{
@POST("/Demo.php")
Call createUser(@Body User user);

=====>>>My Java Class
void submit() {
//The Retrofit builder will have the client attached, in order to get connection logs
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(URL)
.build();

    loginInterFace api = retrofit.create(loginInterFace.class);

    User user = new User(usernameInput.getText().toString().trim(),passwordInput.getText().toString().trim());

    Call<User> apiCall = api.createUser(user);
    apiCall.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Log.e("Call", "" + call);
            Log.e("response", "" + response);

            try {

                User u1=response.body();
                Log.e("response", "" + u1.getSuccess());

            }catch (Exception e){
                Log.e("Error=>",""+e.getMessage());
            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Log.e("Onfailure call", "" + call);
            Log.e("Onfailure Throwable", "" + t);
        }
    });
}

====>>My Model Class

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
* Created by Bhavesh Patel on 28-02-2017.
*/

public class User {
@SerializedName("username")
@Expose
String username;

@SerializedName("password")
@Expose
String password;

@SerializedName("success")
@Expose
String success;

@SerializedName("message")
@Expose
String message;

public User() {
}

public User(final String username, final String password) {
    this.username = username;
    this.password = password;
}

public User(String message, String success, String username, String password) {
    this.message = message;
    this.success = success;
    this.username = username;
    this.password = password;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getSuccess() {
    return success;
}

public void setSuccess(String success) {
    this.success = success;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

Reply