It is necessary to create a POST request with sending a login, password and getting a token, I was recommended to retrofit, since it cannot be done using standard methods. But I do not understand how the request is created in it (the documentation looked), for example

public interface API { @POST("/v1/registration") Response registerUser(); } 

But at least I don’t understand where the link to the site is and where the request parameters are set. Here I see a request to register a user, but where is all the data? Does he take them from the method?

UPD:

  public interface API { @POST("/v1/registration") Response registerUser(@Body RegistrationBody registrationBody); Call<RegistrationResponse> registerUser(); } public class RegistrationBody{ public String login; public String password; } public class RegistrationResponse { public String token; } Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://myserver1.com") .build(); API api = retrofit.create(API.class); @POST("/v1/registration") Call<RegistrationResponse> registerUser(@Body RegistrationBody registrationBody) { return null; } Call<RegistrationResponse> call = api.registerUser(); call.enqueue(new Callback<RegistrationResponse() { @Override public void onResponse(Response<RegistrationResponse> response) { if (response.isSuccess()) { // tasks available } else { // error response, no access to resource? } } 

there are errors starting with call.enqueue

  • one
    To begin with, the Call<RegistrationResponse> registerUser() method must be exactly the same as the normal one, except for the return parameter. - temq

1 answer 1

For example, we have a server with a POST registration method - https://myserver1.com/v1/registration

For example, this method takes a Json of the form:

 { "logins": "ttt", "password": "123" } 

and returns the answer of the form

 { "token":"someToken" } 

Then in the project you need to define an interface that describes this server method in this way

 public interface API { @POST("/v1/registration") RegistrationResponse registerUser(@Body RegistrationBody registrationBody); } 

Where RegistrationBody and RegistrationResponse are classes with the following fields:

 public class RegistrationBody{ public String login; public String password; } public class RegistrationResponse { public String token; } 

Further, to access the server, you must first create a Retrofit object where you need to transfer the server name and any other additional information.

 Retrofit retrofit = Retrofit.Builder() .baseUrl("https://myserver1.com") .build(); 

After, from this object you can get an implementation of the interface with

 API api = retrofit.create(API.class); 

Well, and then on the api object, call the necessary methods. Just remember that you can not climb into the Internet in the main stream. In order to make an appeal to the server in a separate stream, you can use the tools provided by Retrofit. To do this, in the interface, return the parameter to the type Call

 @POST("/v1/registration") Call<RegistrationResponse> registerUser(@Body RegistrationBody registrationBody); 

and then call

 Call<RegistrationResponse> call = api.registerUser(...); call.enqueue(new Callback<RegistrationResponse() { @Override public void onResponse(Response<RegistrationResponse> response) { if (response.isSuccess()) { // tasks available } else { // error response, no access to resource? } } @Override public void onFailure(Throwable t) { } } 

UPD: In the api.registerUser(...) method, you must pass the RegistrationBody object before filling it with data:

 RegistrationBody body = new RegistrationBody(); body.login = "myLogin"; body.password = "12345"; api.registerUser(body); 

Serialization occurs using the GSON library, how to serialize an object correctly, read the official documentation .

To work in asynchronous mode, simply in the API create a method that will return Call<T> instead of T

  • Thank you very much for such a detailed answer. But I did not understand a little 2 points. When creating a second thread, do I need to override the API or create a new interface? and the second question, what should I enter in api.registerUser (...), the login and password variables? - Slava Nenashev
  • @SlavaNenashev completed the answer - temq
  • Now wants me to change to response in the last line. API api = retrofit.create (API.class); Registration Body body = new Registration Body (); @POST ("/ v1 / registration") Call <RegistrationResponse> registerUser (@Body RegistrationBody registrationBody) {body.login = "myLogin"; body.password = "12345"; api.registerUser (body); return null; } Call <RegistrationResponse> call = api.registerUser (body); - Slava Nenashev
  • @SlavaNenashev The Call<RegistrationResponse> registerUser(...) function needs only to be written in the interface, you do not need to implement anything yourself, but only to get the implementation of the API through the retrofit.create(API.class) method retrofit.create(API.class) and call the necessary methods on it. Take a closer look at my examples and examples on the Retrofit site. - temq