Post request using retrofit
You need to create a POST request with sending a username,password and receiving a token, I was recommended to retrofit, since it can't be done using standard methods. But I don't understand how it creates a request (I looked at the documentation), for example
public interface API {
@POST("/v1/registration")
Response registerUser();
}
But I do not understand at least where the link to the site is and where the request parameters are set. Here I see a request for user registration, but where is all the data? does it 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?
}
}
Yes errors starting with call. enqueue
1 answers
For example, we have a server with a POST registration method - https://myserver1.com/v1/registration
For example this method accepts Json of the form:
{
"logins": "ttt",
"password": "123"
}
And returns a response 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;
}
Next, to access the server, you first need to create an object Retrofit
where to pass the name to servers and any other additional information.
Retrofit retrofit = Retrofit.Builder()
.baseUrl("https://myserver1.com")
.build();
Then, from this object, you can get the implementation of the interface with the methods
API api = retrofit.create(API.class);
And then call the necessary methods for the object api
. Just remember that you can not go to the Internet in the main stream. To make the request to the server in a separate thread, you can use the tools that Retrofit provides. To do this, wrap the returned parameter in the interface in 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 object RegistrationBody
after filling it with data:
RegistrationBody body = new RegistrationBody();
body.login = "myLogin";
body.password = "12345";
api.registerUser(body);
Serialization takes place using the GSON library. To learn how to properly serialize an object, read the official documentation.
To work in asynchronous mode, simply create a method in the API
interface that will return Call<T>
instead of T