Volley GET请求发送空参数

时间:2017-05-01 22:06:22

标签: php android android-volley

我制作的Android应用使用Volley来调用PHP脚本来访问MySQL服务器。但是,我通过GET发送的参数为空。当我用Postman测试呼叫时它可以工作。 这是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,密码=测试时,Postman的实际输出):

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

提供相同值时代码的实际输出:

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

1 个答案:

答案 0 :(得分:0)

如果与Volley get请求一起发送,只有参数可以在网址后面拼接。

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