使用服务器和Volley GET参数显示用户详细信息


Working with server and Volley GET parameter to display User Details

我已经有一段时间没有在这里发帖了。现在我被困在这个区域,我必须使用Volley从我的服务器获取数据。我想显示来自服务器的单个数据;但当应用程序运行时,出现了一条错误信息:"所需参数'tag'缺失!"这个错误信息是写在php文件上的,所以我认为我的java活动文件一定有问题。

我真的很感激任何帮助,因为我找不到类似的答案几乎一个星期了。因此,我将在下面发布所涉及的代码。如果有更好的解决方案,请纠正我!学习Android开发真的很有趣,我真的希望我能度过这个难关,当然不是一个人!

getUserDetails从服务器检索数据?虽然我不确定为什么params.put("tag","…")在这里不起作用

 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文件。然而,在运行应用程序时,错误Toast(所需参数"tag"缺失!)告诉我没有找到标签!

    } 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->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;
    }
}

当你发出GET请求时,你必须在url中传递你的请求值,就像AppConfig.URL_USER_DETAILS+"?tag=checkuserdetails";