2016-04-15 71 views
2

我正在做一個android應用程序使用android studio登錄/註冊,我沒有發送註冊信息到.php文件。登錄/註冊應用程序使用volley

這裏是我的代碼:

對於LoginActivity:

import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONException; 
import org.json.JSONObject; 

public class RegisterActivity 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 int age = Integer.parseInt(etAge.getText().toString()); 
      final String password = etPassword.getText().toString(); 

      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(RegisterActivity.this, LoginActivity.class); 
          RegisterActivity.this.startActivity(intent); 
         } else { 
          AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.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(RegisterActivity.this); 
      queue.add(registerRequest); 
     } 
    }); 
} 
} 

對於RegisterActivity:

import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONException; 
import org.json.JSONObject; 

public class RegisterActivity 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 int age = Integer.parseInt(etAge.getText().toString()); 
      final String password = etPassword.getText().toString(); 

      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(RegisterActivity.this, LoginActivity.class); 
          RegisterActivity.this.startActivity(intent); 
         } else { 
          AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.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(RegisterActivity.this); 
      queue.add(registerRequest); 
     } 
    }); 
} 
} 

對於RegisterRequest:

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 = "http://mydomain.hostei.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("age", age + ""); 
    params.put("username", username); 
    params.put("password", password); 
} 

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

而且Register.php:

$con = mysqli_connect("mysql10.000webhost.com", "a3288368_user", "abcd1234", "a3288368_data"); 

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

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

echo json_encode($response); 

我的數據庫和Register.php文件之間的通信正常工作,將值手動放在Register.php文件中。 但是,當我等待值形式POST方法我失敗之間我的應用程序和Register.php

之間進行通信我已經添加了互聯網許可到我的項目,也編譯了排球庫。

任何想法出了什麼問題? 也許我的Java代碼有錯誤?

由於

是在PHP代碼
+0

您是否嘗試過調試代碼?查看請求的實際外觀?你也可以調試php代碼,看看那裏到底是什麼...... – f1sh

+0

什麼都沒有到達php.I呼應變量,並且它們從POST方法中沒有任何價值。看來錯誤發生在java代碼上。 –

+0

通過RegisterReuqest.I正確傳遞的所有參數都已通過system.out.prints檢查過 –

回答

0

問題fixed.The錯誤

我改變回聲json_encode($響應);
with print_r(json_encode($ response));

+0

小提示,你應該在你的php文件的post參數上使用isset()方法來避免錯誤和不必要的錯誤 – Roee

相關問題