What I do:

I send a POST request to the server using Retrofit 1.9.

API:

@FormUrlEncoded @POST("/auth/registration") void register( @Field("name") String name, @Field("surname") String surname, @Field("password") String password, Callback<RegisterResponseNew> response); 

Sending request:

  RestWorker.getInstance().register( "Name", "LastName", "pass", new Callback<RegisterResponseNew>() { @Override public void success(RegisterResponseNew registerResponseNew, Response response) { } @Override public void failure(RetrofitError error) { } }); 

In response, I should get this JSON:

 { "status": "stat", "status_code": "000", "data": { "name": [ "some name" ] } } 

Here are my POJOs:

 public class RegisterResponseNew { @SerializedName("status") private String status; @SerializedName("status_code") private String statusCode; @SerializedName("data") private RegisterDataNew data; } public class RegisterDataNew { @SerializedName("name") @Expose private String name; } 

Problem:

The request is permanently failure, but if removed from the RegisterResponseNew class

  @SerializedName("data") private RegisterDataNew data; 

Then it will, as it should, take place in success.

What could be the problem ?

    1 answer 1

    @Expose is an annotation from the GSon library. This annotation indicates the fields that must be serialized / deserialized. However, this works if the Gson object was created using the builder, i.e. So:

     Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() 

    If the object was created like this:

     Gson gson = new Gson(); 

    then this annotation will have no effect.

    It works like this: if there is a class with this structure:

     public class User { @Expose private String firstName; @Expose(serialize = false) private String lastName; @Expose (serialize = false, deserialize = false) private String emailAddress; private String password; } 

    With the use of excludeFieldsWithoutExposeAnnotation() the password field will not be serialized and deserialized, the lastName and emailAddress fields will not be serialized either. they have a serialize = false flag, plus the entire emailAddress field will not be deserialized, since it has the flag deserialize = false .

    The same effect can be achieved using the transient keyword in front of fields that do not need to be serialized / deserialized.

    It is taken from official documentation .

    UPD : the RegisterDataNew class should be like this, because the type of the name field is JsonArray , therefore it cannot convert JsonArray to the String type, which is why the error crashes. By the way, this should be reflected in the logs.

     public class RegisterDataNew { @SerializedName("name") private List<String> nameList; } 
    • Thank you, I read this dock, but did not understand a bit. Now I will add a question .. - researcher
    • @researcher supplemented the answer - temq
    • @researcher by the way, I think we should ask a new question, because after editing, he completely changed the essence, which is why the initial answer seems meaningless - temq
    • Thanks for the decision! - researcher