How to make a post using Retrofit

Asked

Viewed 539 times

0

I need to create a post in the retrofit to which sending follows this pattern, I have each class separately and a class IDVerification that count all together, and I don’t know which way to mount 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;
}

Facebookprofile class example - (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;
}
  • You need to be more specific. What part is your difficulty in? It is in the interface statement to be passed to Restadapter.Builder?

  • yes, how to create the right interface and how to create the call afterwards in order to pass each right part

  • I need to send to a server the information contained in each class, and those classes are all within this Idverification, I want to know how I declare this, and how I go dividing by class to get something close to the example I gave

  • It is not enough to have a method in the interface with @POST and a parameter with the annotation of @Body? He even tested it?

1 answer

1


within the interface

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

Inside 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 previously used class, 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.