Retrofit all the time



  • What I do:

    I'm sending a POST request to server with 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);
    

    Transmission of 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 have to get this JSON:

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

    Here's my POJO:

    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 a constant failure, but if you get out of class RegisterResponseNew

        @SerializedName("data")
    private RegisterDataNew data;

    Then it'll be as good as it should.

    What's the problem?



  • @Expose This is an annotation from the library. GSon♪ This annotation is marked by fields which must be scattered/degreeized. However, this works if the Gson facility was created with a biller, that is,:

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

    If the object has been created:

    Gson gson = new Gson();
    

    the annotation will have no effect.

    If there's 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;
     }
    

    Use excludeFieldsWithoutExposeAnnotation() Field password There won't becased and disserialized, nor will there be a series of fields lastName and emailAddressthey have a flag. serialize = falsePlus the whole fieldemailAddress It won't be desserial because he's got a flag. deserialize = false

    The same effect can be achieved using the key word transient in front of the floors that don't have to be screaming/degreeing.

    Got it. https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/annotations/Expose.html ♪

    UPDClass RegisterDataNew shall be such as the field type nameJsonArrayTherefore, it cannot convert JsonArray Type Stringthat makes a mistake. By the way, the logs reflected that.

    public class RegisterDataNew {
    
    @SerializedName("name")
    private List&lt;String&gt; nameList;
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2