簡體   English   中英

Volley GET請求發送空參數

[英]Volley GET Request Sending Empty Parameters

我正在制作一個使用Volley來調用PHP腳本以訪問MySQL服務器的Android應用。 但是,我通過GET發送的參數為空。 當我與郵遞員測試通話時,它可以工作。 這是Android代碼:

    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://lastboxusa.com/php/PLogin.php";
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username);
    params.put("password", password);
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, new JSONObject(params), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                VolleyLog.d("Response = " + response.toString());
                String result = response.get("Result").toString();
                VolleyLog.d("Result = " + result);
                if (result.equals("Disallow")) {
                    Toast.makeText(context, "Invalid Username and/or Password.", Toast.LENGTH_LONG).show();
                } else if (result.equals("Customer")) {
                    Toast.makeText(context, "Customer Logged In", Toast.LENGTH_LONG).show();
                    // Customer Login
                } else if (result.equals ("Rep")) {
                    Toast.makeText(context, "Rep Logged In", Toast.LENGTH_LONG).show();
                    // Rep Login
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            try {
                VolleyLog.e("Status Code: ", String.valueOf(error.networkResponse.statusCode));
                VolleyLog.e("Error: ", new String(error.networkResponse.data, "UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }
    };
    queue.add(request);

這是PHP:

header('Content-Type: application/json; charset=utf-8');

$con = mysqli_connect('domain', 'username', 'password', 'db') or die("Connection Failed");

$username = $_GET['username'];
$password = $_GET['password'];

$result = mysqli_query($con, "SELECT * FROM  `Users` WHERE  `Username` =  '$username' AND  `Password` =  '$password'");

if ($result && $result->num_rows > 0) {
    $ans = $result->fetch_assoc()['UserType'];
    $response['Result'] = $ans;
    $response['Username'] = $username;
    $response['Password'] = $password;
} else {
    $response['Result'] = "Disallow";
    $response['Numrows'] = $result->num_rows;
    $response['Username'] = $username;
    $response['Password'] = $password;
}

echo json_encode($response);

mysqli_close($con);

預期的輸出(提供用戶名= testrep,密碼= test時,郵遞員的實際輸出):

{"Result": Rep, "Username": "testrep", "Password", "test"}

提供相同值時代碼的實際輸出:

{"Result": "Disallow", "Numrows": 0, "Username": null, "Password": null}

如果與Volley get請求一起發送,則只能在URL后面拼接參數。

String url="http://lastboxusa.com/php/PLogin.php?username"+username+"&password"+password;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM