我已经在这里发布了一段时间了。 现在我被困在这个区域,我必须使用Volley从我的服务器获取数据。你看我想要从服务器显示个人数据;但是当应用程序运行时,会显示一条错误消息"必需参数'标记'缺少了!"出现了。此错误消息写在php文件上,所以我认为我的java活动文件一定有问题。
我真的很感激任何帮助,因为我差不多一周找不到类似的答案了。所以我将发布下面涉及的代码。如果有更好的解决方案,请纠正我!学习Android开发非常有趣,我真的希望我能够完成这个,而不是一个人!
getUserDetails 从服务器检索数据?虽然我不确定为什么params.put("标签"," ......")在这里不起作用)
public void getUserDetails() {
// Tag to cancel this request:
String tag_string_req = "req_user_profile";
StringRequest stringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_USER_DETAILS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Get User Details Response: " + response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
boolean error = jsonObject.getBoolean("error");
if (!error) {
// Retrieving user's details from database
JSONObject user = jsonObject.getJSONObject("user");
String username = user.getString("username");
String first_name = user.getString("first_name");
String last_name = user.getString("last_name");
String email = user.getString("email");
String type_of_tradesman = user.getString("type_of_tradesman");
sqLiteHandler.getUserDetails(username, email, first_name, last_name,
type_of_tradesman);
profileUsername.setText(username);
} else {
// Error occurred. Getting the error message from index.php
String errorMsg = jsonObject.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(TAG, "User Details Retrieval Error: " + volleyError.getMessage());
Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to URL
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "checkuserdetails");
return params;
}};
// Adding request to the RequestQueue
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
}
在上一段代码中,您会注意到我在这里使用了 SQLiteHandler的getUserDetails 。它看起来很有帮助,但我不知道这是否需要在这里。
public HashMap<String, String> getUserDetails(String username, String email, String first_name,
String last_name, String type_of_tradesman) {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("username", cursor.getString(1));
user.put("first_name", cursor.getString(2));
user.put("last_name", cursor.getString(3));
user.put("email", cursor.getString(4));
user.put("type_of_tradesman", cursor.getString(5));
user.put("uid", cursor.getString(6));
user.put("created_at", cursor.getString(7));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
最后,php文件有助于确定&#34;标签&#34;请求。但是,在运行应用程序时,错误Toast(必需参数&#39;标记&#39;丢失!)告诉我没有找到任何标记!
} else if ($tag == 'checkuserdetails') {
$user = $db->getUserDetails($username, $email, $first_name, $last_name, $date_of_birth, $type_of_tradesman);
if ($user != false) {
// User found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["username"] = $user["username"];
$response["user"]["email"] = $user["email"];
$response["user"]["first_name"] = $user["first_name"];
$response["user"]["last_name"] = $user["last_name"];
$response["user"]["type_of_tradesman"] = $user["type_of_tradesman"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// Or else if user is not found
$response["error"] = TRUE;
echo json_encode($response);
}
...
...
...
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
$ db-&gt; getUserDetails函数如下:
/**
* Retrieve user's details from the database
*/
public function getUserDetails($username, $email, $first_name, $last_name, $date_of_birth, $type_of_tradesman) {
$uuid = uniqid('', true);
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// check for successful store
if ($result) {
// Get user details
$result = mysql_fetch_array($result);
$user = array();
$user["uid"] = $result["uid"];
$user["username"] = $result["username"];
$user["email"] = $result["email"];
$user["first_name"] = $result["first_name"];
$user["last_name"] = $result["last_name"];
$user["type_of_tradesman"] = $result["type_of_tradesman"];
} else {
return false;
}
}
答案 0 :(得分:0)
当你发出GET请求时,你必须在url中传递请求值,只有这样AppConfig.URL_USER_DETAILS+"?tag=checkuserdetails";