[英]'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.