Volley POST请求收到错误的结果

时间:2016-06-27 11:35:01

标签: android api rest android-volley postman

我在在线服务器上托管了我的API。当我使用Postman应用程序测试它时,它会返回正确的结果(即数据库中用户的详细信息)但是下面的Volley请求得到了错误的结果(即缺少必需的参数);

邮递员请求的屏幕截图

Postman Test Screenshot

排球请求

private void loginUser(final String username, final String password){

        String URL_REGISTER = "http://www.h8pathak.orgfree.com/jobs/login.php";
        pDialog.setMessage("Logging in...");
        showDialog();


        StringRequest strReq = new StringRequest(Request.Method.POST,
                URL_REGISTER, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                hideDialog();
                try {
                    JSONObject jObj = new JSONObject(response);
                    int result = jObj.getInt("result");


                    if(result==1){

                        session.setLogin(true);

                        JSONObject jObj2 = jObj.getJSONObject("user");
                        Intent i = new Intent(LoginActivity.this, EmployerActivity.class);
                        i.putExtra("username", jObj2.getString("username"));
                        i.putExtra("email", jObj2.getString("email"));
                        startActivity(i);
                        finish();
                        Toast.makeText(LoginActivity.this, jObj.getString("message"), Toast.LENGTH_SHORT).show();
                    }

                    else{
                        Toast.makeText(LoginActivity.this, jObj.getString("message"),Toast.LENGTH_SHORT).show();

                    }
                }

                catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, volleyError.getMessage(), Toast.LENGTH_SHORT).show();
                hideDialog();
            }
        }){

            @Override
            protected Map<String, String> getParams()  {

                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                params.put("password", password);

                return params;
            }
        };

        AppController.getInstance().addToRequestQueue(strReq);

    }

PHP代码以接收请求参数并返回相应的响应。

if(isset($_POST['username']) && isset($_POST['password']))  {

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

      //Get the details from the database and echo in a json response

}

else{

    $message = "Required parameters missing!";
    $response['result']=0;
    $response['message']=$message;

    echo json_encode($response);
}   

可能是什么错误?

2 个答案:

答案 0 :(得分:3)

您的服务器配置出现问题:http://www.h8pathak...未按预期响应,但http://h8pathak...有效。后者用于你的邮递员示例,在你的Android代码中使用它,它也可以在那里工作。

答案 1 :(得分:0)

WWW就是答案。这一定是个bug。它转到URL并运行它但不发布POST。

http://www.example.com/myphpfile.php错了。

http://example.com/myphpfile.php是对的。