How to make a post using Retrofit

0

I need to create a post in the retrofit to which the submission follows this pattern, I have each class separately and a class IDVerification that contains all together, and I do not know how to amount this post

{
 "facebookProfile":{
  "name":"String",
  "education_history":"String",
  "birthday":"String",
  "hometown":"String",
  "email":"String",
  "location":"String"
},
 "picture":{
  "selfieURL":"String",
  "documentIDs":{
     "frontURL":"String",
     "backURL":"String"
      }
 }
}

IDVerification -

public class IDVerification {

    private FacebookProfile facebookProfile;

    private Picture picture;
}

class example FacebookProfile - (already contains getters and setters)

public class FacebookProfile implements Serializable{

    private String name;

    private String education_history;

    private String birthday;

    private String hometown;

    private String email;

    private String location;
}
    
asked by anonymous 26.01.2016 / 20:24

1 answer

1

within the interface

@POST("/data")
Call<IDVerification> postCustomerConfirmation (@Body IDVerification idVerification);

Within the ActivityUI onCreate

Retrofit client = new Retrofit.Builder()
            .baseUrl(APIForid.base_url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

Calling a control class or asynchronous use

public void sendIDVerification(IDVerification idVerification) {
    sendIDVerificationUI.showSending();
    if(idVerification != null) {
        Call<IDVerification> call = apiForid.postCustomerConfirmation(idVerification);
        call.enqueue(new Callback<IDVerification>() {
            @Override
            public void onResponse(Response<IDVerification> response, Retrofit retrofit) {
                if (response.isSuccess()) {
                    sendIDVerificationUI.finishSendingVerification();
                }
            }

            @Override
            public void onFailure(Throwable t) {
                if(t.toString() != null) {
                    String message = t.getMessage();
                    if (message.contains("timeout") || message.contains("ETIMEDOUT")) {

                        sendIDVerificationUI.onSendIdVerificationError(0);

                    } else if (message.contains("Unable to resolve host")) {

                        sendIDVerificationUI.onSendIdVerificationError(0);

                    } else if (message.contains("Failed to connect")) {

                        sendIDVerificationUI.onSendIdVerificationError(0);

                    } else if (message.contains("SocketTimeout") ){

                        sendIDVerificationUI.onSendIdVerificationError(0);
                    }
                }
            }
        });
    }
}

Within the IDVerification class, create a constructor

public IDVerification(FacebookProfile facebookProfile, Customer customer, Location location, Picture picture, Audio audio) {
    if(facebookProfile != null) {
        this.facebookProfile = new FacebookProfile(facebookProfile.getName(),
                facebookProfile.getEducation_history(),
                facebookProfile.getBirthday(),
                facebookProfile.getHometown(),
                facebookProfile.getEmail(),
                facebookProfile.getLocation());
    }
...

Within each class used previously, have your own constructor

public FacebookProfile (String name, String education_history, String birthday, String hometown, String email, String location){
    this.name = name;
    this.education_history = education_history;
    this.birthday = birthday;
    this.hometown = hometown;
    this.email = email;
    this.location = location;
}
    
04.02.2016 / 22:23