Retrofit: Retrofit 2: Object Attributes null, Using GsonConverterFactory, StatusCode 200

Created on 25 Apr 2016  路  6Comments  路  Source: square/retrofit

Hi,
today is started using Retrofit 2 (up from 1.6.1).
I want to request a User Object from Server. Via Retrofit i getting an User Object with all Attributes wich are null and StatusCode 200. Also when i using an intercepter the error occurs.
can you help me or say what is the error with this?

build.gradle imports

    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Service:

@GET(Urls.LOGIN_URL)
Call<RetrofitUser> getUserDataAsUserObject(@Query("loginname") String userName, @Query("password") String password);

Retrofit initialization

    private static String JSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

    public static RetrofitService getUserService() {
        RetrofitService service;
        synchronized (RetrofitClientProvider.class) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Urls.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(getGsonViaBuilder()))
                    .build();
            service = retrofit.create(RetrofitService.class);
        }
        return service;
    }

    private static Gson getGsonViaBuilder() {
        return new GsonBuilder().setDateFormat(JSON_DATE_FORMAT).create();
    }

UserObject

public class RetrofitUser  {

    @SerializedName("firstname")
    private String firstname;
    @SerializedName("lastname")
    private String lastname;
    @SerializedName("email")
    private String email;
    @SerializedName("username")
    private String username;
    @SerializedName("uid")
    private Integer uid;
    @SerializedName("picture")
    private String pictureUrl;
    @SerializedName("usertoken")
    private String usertoken;

    /**
     * @return The firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @return The lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @return The email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return The username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return The uid
     */
    public Integer getUid() {
        return uid;
    }

    public String getPicture() {
        return pictureUrl;
    }

    /**
     * @return The usertoken
     */
    public String getUsertoken() {
        return usertoken;
    }


    @Override
    public String toString() {
        return "Firstname: " + getFirstname() + " - Lastname: " + getLastname() + " - Username: " + getUsername() + " - E_Mail: " + getEmail();
    }
}

Fragment, which Requests the Data

public class RetrofitFragment extends Fragment{

    private static final String TAG = RetrofitFragment.class.getSimpleName();

    @Bind(R.id.fragment_retrofit_2_requested_username)
    TextView requestedUsername;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_retrofit_2, container, false);
        ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    @OnClick(R.id.fragment_retrofit_2_requestButton)
    protected void requestData() {
        RetrofitService service = RetrofitClientProvider.getUserService();
        Call<RetrofitUser> call = service.getUserDataAsUserObject("Mr_T", "test");
        call.enqueue(new Callback<RetrofitUser>() {
            @Override
            public void onResponse(Call<RetrofitUser> call, Response<RetrofitUser> response) {
                  response.body() <<--Attributes are null
            }

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

            }
        });
    }

Json

{
  "user": {
    "uid": 11,
    "firstname": "Mr",
    "lastname": "T-Est",
    "email": "[email protected]",
    "username": "Mr_T",
    "picture": null,
    "usertoken": "3dca80822788210d4ab96220ffe5c3068ab0422cf7c702c0115d88a28e25c51fcf6f10ce"
  }
}

Most helpful comment

The fields you need to bind to are within the "user" key of the JSON not at the root. Create an object whose user field is a RetrofitUser so that the Java model matches the JSON model.

All 6 comments

response.body() returns a RetrofitUser in your case. Grab that and then access the needed fields.

the Problem is, that all field return null if i access them ...

The fields you need to bind to are within the "user" key of the JSON not at the root. Create an object whose user field is a RetrofitUser so that the Java model matches the JSON model.

Hi! I have a problem . So it,
I have a Object Ex :
Order
class Order{
@SerializedName("o_id")
int ID;
@SerializedName("o_name")
String o_name="";
@SerializedName("videos")
List

I'm not getting object data in object. Please help me

@omurburuk People can help on StackOverflow. Use the Gson tag there.

Hello, got problem using retrofit. a compilation error saying cannot access javax.annotation.Nullable.
I spend the whole afternoon fixing this one.

Was this page helpful?
0 / 5 - 0 ratings