即时创建连接到MYSQL数据库的应用程序,用户可以登录或注册。在寄存器类中,我使用OKHTTP3注册一个新用户。 OKHTTP3向执行查询的php页面发出请求。页面的响应始终相同(代码:200,messagge:" empty")。我喜欢自定义php页面的响应,如代码和messagge,所以我可以知道注册是否成功。我该怎么办?
请尽可能简单。
我的OKHTTP3请求:
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("");
}
}
});
}
});
我的php页面:
<?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{
}
?>
答案 0 :(得分:1)
您可以使用http_response_code();
设置输出的代码,例如:
http_response_code(500);
如果发生服务器错误,则400表示错误请求等。对于正文,您应该能够echo
正确地存在于正文中。我建议您做一些像json_encode()
这样的数据。
请不要使用PHP关闭标签。