在Android中使用Volley库,我使用POST方法将数据发送到服务器。
private void registerNewUser(final String name, final String email, final String password) {
final String TAG_REGISTER = "New user registration";
String tag_string_req = "req_register";
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setCancelable(true);
pDialog.setMessage("Registering..");
pDialog.show();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, ServerURL.URL_REGISTER, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG_REGISTER, "Register Response: " + response.toString());
pDialog.dismiss();
try {
JSONObject jObj = new JSONObject(response.toString());
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUserIntoSQLite(name, email, uid, created_at);
Toast.makeText(getActivity(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG_REGISTER, "Registration Error: " + error.getMessage());
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "register");
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
ApplicationController.getInstance().addToRequestQueue(jsonObjectRequest, tag_string_req);
}
这是获取POST数据的PHP部分。
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'mysql/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "error" => FALSE);
// check for tag type
if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = TRUE;
$response["error_msg"] = "User already exists";
echo json_encode($response);
} else {
// store user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Error occured in Registartion";
echo json_encode($response);
}
}
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
?>
现在,当我在这里运行registerNewUser()方法时,它会显示标签丢失的Toast消息。正如您在PHP代码中看到的那样,错误消息&#34;必需参数&#39;标记&#39;缺少了!&#34;如果标记未设置或为空,则显示。
为什么即使我在Android代码中正确包含了标记变量,我也会收到该消息?
编辑:
我刚从Web服务器下载了.htaccess文件。我目前正在使用000webhost.com作为网络托管,它就像文件中的内容一样。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
这些是我完全不知道的..这部分是否有错误阻碍了PHP中的POST请求?如果是这样,我该如何解决?