POST请求排球

问题描述 投票:0回答:2

我有个问题。

当我提交请求时,显示错误“E / Volley:[300] BasicNetwork.performRequest:http://192.168.0.130:1010/api/CRM/check_login的意外响应代码404”。

但我发送相同的请求与浏览器或POSTMAN(http://192.168.0.130:1010/api/CRM/check_login?username=test&password=123),它的工作原理。

你可以查看请求的照片(https://www.dropbox.com/sh/vme97ftuifwb3l0/AADZonU2gZRlLjxV64C2iapFa?dl=0

Error

POSTMAN

btn_volley.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            String URL = "http://192.168.0.130:1010/api/CRM/check_login";


            Response.Listener<String> listener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.isEmpty()) {
                        Toast.makeText(getApplicationContext(), "test",Toast.LENGTH_LONG).show();
                    } else {

                            Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                    }
                }
            };


            Response.ErrorListener errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                           Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
                }
            };



            StringRequest request = new StringRequest(Request.Method.POST, URL, listener, errorListener)
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {

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

                }
            };

            AppController.getInstance().addToRequestQueue(request);

        }
    });
java android rest api web-services
2个回答
1
投票

您可以直接使用您的网址http://192.168.0.130:1010/api/CRM/check_login?username=test&password=123

因为getParams()方法用于application/form-data而不是用于Query params所以它对你不起作用。

资源 :

btn_volley.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String username="test";//you can get username and password from edittexts
        String password="123";

        String URL = "http://192.168.0.130:1010/api/CRM/check_login?username="+username+"&password="+password;

        Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "test",Toast.LENGTH_LONG).show();
                } else {
                       Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                }
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                       Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
            }
        };

        StringRequest request = new StringRequest(Request.Method.POST, URL, listener, errorListener);

        AppController.getInstance().addToRequestQueue(request);

    }
});

0
投票

您需要将Content-Type添加到标题中。

@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;
}
© www.soinside.com 2019 - 2024. All rights reserved.