我的android应用程序中有一个注册活动,并使用android volley我调用服务器上的PHP文件。
输入详细信息并按Enter后,将收到以下响应。
02-15 12:59:09.939: D/RegisterActivity(377): Register Response: <br />
02-15 12:59:09.939: D/RegisterActivity(377): <b>Notice</b>: Undefined index: name in <b>D:\www\students\project\register.php</b> on line <b>12</b><br />
02-15 12:59:09.939: D/RegisterActivity(377): <br />
02-15 12:59:09.939: D/RegisterActivity(377): <b>Notice</b>: Undefined index: email in <b>D:\www\students\project\register.php</b> on line <b>13</b><br />
02-15 12:59:09.939: D/RegisterActivity(377): <br />
02-15 12:59:09.939: D/RegisterActivity(377): <b>Notice</b>: Undefined index: dob in <b>D:\www\students\project\register.php</b> on line <b>14</b><br />
02-15 12:59:09.939: D/RegisterActivity(377): <br />
02-15 12:59:09.939: D/RegisterActivity(377): <b>Notice</b>: Undefined index: gender in <b>D:\www\students\project\register.php</b> on line <b>15</b><br />
02-15 12:59:09.939: D/RegisterActivity(377): <br />
02-15 12:59:09.939: D/RegisterActivity(377): <b>Notice</b>: Undefined index: password in <b>D:\www\students\project\register.php</b> on line <b>16</b><br />
02-15 12:59:09.939: D/RegisterActivity(377): {"error":true,"error_msg":"Required Parameters (name, email, gender, dob, password) missing"}
显然结果意味着什么都没有通过。
所以这是我的注册活动,在那里,使用凌空,请求。
private void registerUser(final String name, final String email, final String dob, final String gender, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,ApplicationServicesConfig.Register_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String dob = user.getString("dob");
String gender = user.getString("gender");
String created_at = user.getString("created_at");
// Inserting row in users table
db.addUser(name, email, dob, gender, created_at);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("dob", dob);
params.put("gender", gender);
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Basic Z2FycmV0dGg6ZnJBc3Rpbmc0");
return params;
}
};
Log.d(TAG, "STRING REQUEST: " + strReq.getUrl());
// Adding request to request queue
ApplicationController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
据我所知,地图会将所有正确的参数映射到网址。
通常情况下,我是使用HTTP请求来做的,但是读取凌空是一种更清洁的方式。 无论如何,我也像往常一样直接调用了传递到最后的参数的url 例如?name = bob&amp; age等等
有没有办法可以打印网址凌空调用或它添加的参数?
由于
编辑PHP:
<?php
/**
*/
require_once 'helpers/db_functions.php';
$db = new db_functions();
$response = array("error" => FALSE);
$name = $_GET['name'];
$email = $_GET['email'];
$dob = $_GET['dob'];
$gender = $_GET['gender'];
$password = $_GET['password'];
$string = $name + $email + $dob + $gender + $password;
json_encode($string);
if (isset($_GET['name']) && isset($_GET['email']) && isset($_GET['dob']) && isset($_GET['gender']) && isset($_GET['password'])){
$name = $_GET['name'];
$email = $_GET['email'];
$dob = $_GET['dob'];
$gender = $_GET['gender'];
$password = $_GET['password'];
if ($db->isUserExisted($email)){
$response["error"] = TRUE;
$response["error_msg"] = "User email already exists = " . $email;
echo json_encode($response);
}else{
$user = $db->insertUser($name,$email,$gender,$dob,$password);
if ($user){
$response["error"] = FALSE;
$repsonse["user"]["id"] = $user["id"];
$repsonse["user"]["name"] = $user["name"];
$repsonse["user"]["email"] = $user["email"];
$repsonse["user"]["dob"] = $user["dob"];
$repsonse["user"]["gender"] = $user["gender"];
$response["user"]["created_at"] = $user["created_at"];
echo json_encode($response);
}else{
$response["error"] = TRUE;
$response["error_msg"] = "Problem when registering user";
echo json_encode($response);
}
}
}else{
$response["error"] = TRUE;
$response["error_msg"] = "Required Parameters (name, email, gender, dob, password) missing";
echo json_encode($response);
}
?>
答案 0 :(得分:0)
确定。你已经发布了php文件。你可以清楚地看到它需要一个GET方法。因此,请指示Volley现在执行GET请求而不是POST请求。
答案 1 :(得分:0)
我发现StringRequest
的语法错误,
StringRequest stringRequest = new StringRequest( API.BOOKING_COMPLETE_API, new Response.Listener<String>() {
此处缺少第一个参数StringRequest.Method.POST
。
答案 2 :(得分:0)
由于将方法更改为isset( $_POST['variable'] )
,因此您的PHP脚本应测试$_GET[]
,而不是POST
。