2016-11-07 50 views
2

我已經使用000webhost創建了一個數據庫,並將我的php文件上傳到了那裏的服務器。使用android studio,我試圖創建一個用戶註冊頁面。我點擊進入註冊頁面,輸入所有信息,點擊提交按鈕,它會將我帶回登錄頁面,但它應該這樣做,信息不會發送到我的數據庫。我沒有收到和錯誤消息,所以我不知道從哪裏開始。以下是我的java類和php文件中的代碼以及清單文件的一部分。如何使用PHP和android studio(服務器00webhost)將數據導入數據庫?

寄存器請求的Java類

package amp.riot; 

import com.android.volley.Response; 
import com.android.volley.toolbox.StringRequest; 

import java.util.HashMap; 
import java.util.Map; 


public class RegisterRequest extends StringRequest { 
    private static final String REGISTER_REQUEST_URL ="https://upbeat.000webhostapp.com/register.php"; 
    private Map<String, String> params; 

    public RegisterRequest(String name, String username, int age, String password,Response.Listener<String> listener){ 
     super (Method.POST, REGISTER_REQUEST_URL, listener, null); 
     params = new HashMap<>(); 
     params.put("name", name); 
     params.put("username", username); 
     params.put("password", password); 
     params.put("age", age + ""); 
    } 

    public Map<String, String> getParams(){ 
     return params; 
    } 

} 

寄存器頁java類

public class Register extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 

    final EditText etAge= (EditText) findViewById(R.id.etAge); 
    final EditText etName= (EditText) findViewById(R.id.etName); 
    final EditText etUsername= (EditText) findViewById(R.id.etUsername); 
    final EditText etPassword= (EditText) findViewById(R.id.etPassword); 
    final Button bRegister= (Button) findViewById(R.id.bRegister); 

    bRegister.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      final String name=etName.getText().toString(); 
      final String username=etUsername.getText().toString(); 
      final String password=etPassword.getText().toString(); 
      final int age= Integer.parseInt(etAge.getText().toString()); 

      final Response.Listener<String> responseListener = new Response.Listener<String>(){ 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonResponse = new JSONObject(response); 
         boolean success = jsonResponse.getBoolean("success"); 

         if (success) { 
          Intent intent = new Intent(Register.this, Login.class); 
          Register.this.startActivity(intent); 
         } else { 
          AlertDialog.Builder builder = new AlertDialog.Builder(Register.this); 
          builder.setMessage("Register Failed") 
            .setNegativeButton("Retry", null) 
            .create() 
            .show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 
      RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener); 
      RequestQueue queue = Volley.newRequestQueue(Register.this); 
      queue.add(registerRequest); 
     } 
    }); 

} 

}

清單文件

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="amp.riot"> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

註冊PHP文件

<?php 
$con = mysqli_connect("localhost", "id202020_sierra", "05alop59", "id202020_plank"); 

$name = $_POST["name"]; 
$age = $_POST["age"]; 
$username = $_POST["username"]; 
$password = $_POST["password"]; 
$statement = mysqli_prepare($con, "INSERT INTO user(name, username, age, password) VALUES (?, ?, ?, ?)"); 
mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password); 
mysqli_stmt_execute($statement); 

$response = array(); 
$response["success"] = true; 
echo json_encode($response); 

?>

+0

你說你收到一條錯誤消息,我們需要它。 – zack6849

+0

對不起,我的意思是說沒有錯誤信息,這就是爲什麼我不知道該怎麼做。 – Bry

回答

0

嘗試StringRequest而是採用Response.Listener<String>

StringRequest strreq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show(); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 

    } 
}){ 
    @Override 
    protected Map<String, String> getParams() throws AuthFailureError { 
     Map<String,String> params = new HashMap<String, String>(); 

     params.put("id",id); 
     params.put("name",editText_name.getText().toString()); 
     params.put("pass",editText_pass.getText().toString()); 
     return params; 
    } 
}; 
AppController.getInstance().addToRequestQueue(strreq); 
0

你使你的PHP文件中的小的失誤。 變化,

mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password); 

要,

mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password); 

這裏,s表示字符串,i表示整數。我相信用戶名是String,年齡是int。謝謝。

相關問題