im creating a application connected to a MYSQL Database where user can login or register. In the register class i use OKHTTP3 to register a new user. OKHTTP3 make a request to a php page that execute the query. The response of the page is always the same (code: 200, messagge:"empty" ). Id like to custom the response of the php page like the code and messagge so i can know if the registration was success or not. How can i do it?
As simple as possible please.
My OKHTTP3 request:
String reg_name = username.getText().toString();
String reg_pass = password.getText().toString();
String reg_url = "https://collectcards.000webhostapp.com/register.php";
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("user_name", reg_name)
.add("user_pass", reg_pass)
.build();
Request request = new Request.Builder()
.url(reg_url)
.post(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Request failed
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Handle whatever comes back from the server
String res = response.message(); //MESSAGE IS ALWAYS EMPTY, COSE IS ALWAYS 200
if(res.equals("OK")) { //ID LIKE TO CUSTOM THE MESSAGGE TO SAY OK WHEN IT GOES WELL AND NOTOK WHEN IT FAIL
ok = true;
risposta = "Registrazione Avvenuta";
}else {
risposta = "Registrazione Fallita";
ok = false;
}
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(thisContext, risposta, Toast.LENGTH_LONG).show();
if (ok) {
Intent myIntent = new Intent(thisContext, mainMenuActivity.class);
thisContext.startActivity(myIntent);
}else{
password.setText("");
username.setText("");
}
}
});
}
});
My php page:
<?php
$connessione = mysqli_connect("localhost", "usr", "psw", "db");
$user_name = $_POST['user_name'];
$user_pass = $_POST['user_pass'];
$query = "INSERT INTO `player` (`username`, `password`, `money`) VALUES ('".$user_name."', '".$user_pass."', '100');";
$risultato = mysqli_query($connessione, $query);
if($risultato){
//CUSTOM MESSAGGE
}else{
}
?>