Answer the question
In order to leave comments, you need to log in
How to get a response from the server that the user exists in the database?
I want to implement registration from the application. There is RegisterActivity, and when the user enters data, they are sent to my server, processed through PHP and entered into the database. I created a Server class that sends my data in a POST request (via the OkHTTP library) and I'm wondering if PHP responds, let's say that such a user is already in the database, how can I return this response to the user? How to get basically what PHP will answer? Perhaps through the response object? I'm still swimming in Android Class
code:
public class Server {
public static final String address = "адрес сервера тут";
public static void register(String username, String password) {
OkHttpClient client = new OkHttpClient();
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add("username", username);
formBuilder.add("password", password);
RequestBody body = formBuilder.build();
Request request = new Request.Builder().url(Server.address).post(body).build();
try {
Response response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
there are two ways
through response.body() response. something specific
, and through the server response code response.code()
, you need to take into account that isSuccessful() is codes 200-299
, it is also desirable to check in isSuccessful() responses. You never know.
for reflection www.restapitutorial.ru/httpstatuscodes.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question