普通网友 2017-05-01 22:06
浏览 16
已采纳

too long

I'm making an Android app that uses Volley to make a call to a PHP script to access a MySQL server. However, the parameters that I'm sending via GET are empty. It works when I test the call with Postman. Here's the Android code:

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

Here's the 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);

Expected output of this (Actual output from Postman when supplied username=testrep, password=test):

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

Actual output from code when supplied with the same values:

{"Result": "Disallow", "Numrows": 0, "Username": null, "Password": null}
  • 写回答

1条回答 默认 最新

  • dou72260 2017-05-02 03:12
    关注

    If sent with Volley get request,only the parameters can be stitched behind the url.

    String url="http://lastboxusa.com/php/PLogin.php?username"+username+"&password"+password;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法