简体   繁体   English

'如何使用带有“Content-Type”的 Volley 发送 Post 请求:“application/x-www-form-urlencoded”

[英]'How to send Post request using Volley with “Content-Type”:“application/x-www-form-urlencoded”

I am trying to send a POST Request using Volley with the following:我正在尝试使用 Volley 发送具有以下内容的 POST 请求:

headers:标题:

{
"Content-Type": "application/x-www-form-urlencoded"
}

(Form Data) (表格数据)

grant_type=password&client_id=acme&client_secret=acmesecret&username=ayan@vvv.in&password=%2523

But I am always getting this error: E/Volley: [26219] BasicNetwork.performRequest: Unexpected response code 400但我总是收到此错误: E/Volley: [26219] BasicNetwork.performRequest: Unexpected response code 400

Here is my code这是我的代码

 RequestQueue queue = Volley.newRequestQueue(mContext);
    StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, getResources().getString(R.string.base_url), new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            String resp = response.toString();
            Log.e(TAG,"Response***"+resp);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            return params;
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("grant_type", "password");
            params.put("client_id", "acme");
            params.put("client_secret", "acmesecret");
            params.put("username", "ayan@vvv.in");
            params.put("password", "%2523");
            return params;
        }

    };
    queue.add(jsonObjRequest);
}

There are several causes to have有几个原因

Volley: [26219] BasicNetwork.performRequest: Unexpected response code 400排球:[26219] BasicNetwork.performRequest:意外响应代码 400

You can check factors given in the below您可以检查下面给出的因素

1.If URL contains blank space then add this code to be safe 1.如果 URL 包含空格,则添加此代码以确保安全

String url = "Your URL Link";

url = url.replaceAll(" ", "%20");

StringRequest stringRequest = new StringRequest(Request.Method.POST, getResources().getString(R.string.base_url),
                        new com.android.volley.Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                            ...
                            ...
                            ...

2.Also, you can try by appending an extra '/' to your URL, eg: 2.此外,您可以尝试在 URL 中附加一个额外的“/”,例如:

String url = "http://www.google.com" 

To

String url = "http://www.google.com/"

3.You can do something similar to this in your error listener to check for the response data in the VolleyError and parse it your self. 3.您可以在错误侦听器中执行类似的操作,以检查 VolleyError 中的响应数据并自行解析。

    /* import com.android.volley.toolbox.HttpHeaderParser; */
    public void onErrorResponse(VolleyError error) {

    // As of f605da3 the following should work
    NetworkResponse response = error.networkResponse;
    if (error instanceof ServerError && response != null) {
        try {
            String res = new String(response.data,
                       HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            // Now you can use any deserializer to make sense of data
            JSONObject obj = new JSONObject(res);
        } catch (UnsupportedEncodingException e1) {
            // Couldn't properly decode data to string
            e1.printStackTrace();
        } catch (JSONException e2) {
            // returned data is not JSONObject?
            e2.printStackTrace();
        }
    }
  }

4.You need to override getBodyContentType() and return application/x-www-form-urlencoded; 4.需要重写getBodyContentType()返回application/x-www-form-urlencoded; charset=UTF-8.字符集=UTF-8。

StringRequest jsonObjRequest = new StringRequest(

    Request.Method.POST,
    getResources().getString(R.string.base_url),
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            MyFunctions.toastShort(LoginActivity.this, response);
        }
    }, 
    new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("volley", "Error: " + error.getMessage());
            error.printStackTrace();
            MyFunctions.croutonAlert(LoginActivity.this,
                MyFunctions.parseVolleyError(error));
            loading.setVisibility(View.GONE);
        }
    }) {

    @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", etUname.getText().toString().trim());
        params.put("password", etPass.getText().toString().trim());
        return params;
    }

};

AppController.getInstance().addToRequestQueue(jsonObjRequest);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用内容类型 x-www-form-urlencoded android 改造发送帖子请求 - How to send post request with content-type x-www-form-urlencoded android retrofit 如何获取登录 api 的响应以使用 volley 对内容类型为 application/x-www-form-urlencoded 的此 json 数据执行登录 - How to get response for login api to perform login using volley for this json data with Content-type: application/x-www-form-urlencoded 更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML - Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded 如何使用“ Content-type:application / x-www-form-urlencoded”发出Okhttp请求? - How to make an Okhttp Request with “Content-type:application/x-www-form-urlencoded”? 为什么发生okhttp的Post方法错误[不支持HTTP请求中指定的Content-Type头:application / x-www-form-urlencoded]? - Why Post method of okhttp occur error [Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded]? 内容类型为 application/x-www-form-urlencoded 的 HTTP Post 请求在 Spring 引导服务中不起作用 - HTTP Post request with content type application/x-www-form-urlencoded not working in Spring boot service 如何将此json发送到内容类型为android中的application / x-www-form-urlencoded的服务器 - How can send this json to server with content type as application/x-www-form-urlencoded in android 为什么Volley String请求不能在x-www-form-urlencoded中使用POST方法发送Body参数? - why can't Volley String request send Body parameters with POST method in x-www-form-urlencoded? 如何使用Android批注和RestTemplate在POST请求的正文中发送x-www-form-urlencoded - How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate 如何使用内容类型为x-www-form-urlencoded的okhttp库发布数据? - How do I post data using okhttp library with content type x-www-form-urlencoded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM