我在Android中有一个简单注册表单的注册脚本。首先,我使用Volley库作为前端部分。
private void registerUser(final String firstName, final String lastName, final String email, String password) {
String tag_json_obj = "json_obj_req";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("posting...");
pDialog.show();
final HashMap<String, String> postParams = new HashMap<String, String>
();
postParams.put("firstName", firstName);
postParams.put("lastName", lastName);
postParams.put("email", email);
postParams.put("password", password);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_REGISTER, jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
//Toast.makeText(getApplicationContext(), response.getString("message"), Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Thank you for your post", Toast.LENGTH_LONG).show();
if (response.getString("status").equals("fail")) {
//session.setLogin(true);
pDialog.dismiss();
Toast.makeText(getApplicationContext(),response.getString("message"), Toast.LENGTH_SHORT).show();
//startActivity(intent);
//finish();
}else{
Toast.makeText(getApplicationContext(), response.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Log.e("TAG", e.toString());
}
pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
这部分工作正常,因为我可以得到JSON。
{"status":"fail","message":"Missing POST parameter(s)"}
PHP脚本是。
<?php
require 'connect.php';
require 'functions.php';
if(logged_in())
{
header("location:profile.php");
exit();
}
if (isset($_POST['firstName']) && isset($_POST['lastName']) &&
isset($_POST['email']) && isset($_POST['password'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$password = $_POST['password'];
$stmt = $con->prepare('
INSERT INTO
users
(firstName,lastName,email,password)
VALUES
(?,?,?,?)
');
if ( !$stmt ) {
$result = array('status'=>'fail', 'message'=>'prepare failed');
}
else if ( !$stmt->bind_param('ssss', $firstName, $lastName, $email, $password) ) {
$result = array('status'=>'fail', 'message'=>'bind failed');
}
else if ( !$stmt->execute() ) {
$result = array('status'=>'fail', 'message'=>'execute/insert failed');
}
else {
$result = array('status'=>'success', 'message'=>'Successfully
registered');
}
}
else {
$result = array('status'=>'fail', 'message'=>'Missing POST
parameter(s)');
}
echo json_encode($result);
?>
问题是没有设置$ _POST变量,这就是我得到JSON响应的原因。我知道对于html表单你设置post变量如$ _POST ['输入字段的名称'],但在我的情况下我不想要那样。