android - Volley POST 请求收到错误结果

标签 android api rest android-volley postman

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

postman 请求的屏幕截图

Postman Test Screenshot

Volley 请求

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

可能的错误是什么?

最佳答案

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

关于android - Volley POST 请求收到错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38052917/

相关文章:

android - 根据主题设置颜色

c++ - Windows api,是用C还是C++写的?

json - 在 VBA 中发送 JSON POST 请求

security - 带有 REST 的基于角色的 Web UI

java - LayoutParams 需要 API 19?

android - android中的gps定位

android - RxJava 分页

javascript - 隐藏 Google 登录 "Welcome back, {name}"

java - Jersey 休息客户端不添加查询参数

java - 通过 Spring MVC 将对象转换为 REST 服务中的 JSON