带标题和Parametes Volley的删除请求 [英] Delete Request With header and Parametes Volley
问题描述
我想使用Volley连同标题和正文参数一起向服务器发送删除请求.但我无法成功发送请求
Hi i want to Send Delete Request to server using Volley along Headers and body parameters. but i am not able to send request successfully
我尝试过的事情
JSONObject jsonbObjj = new JSONObject();
try {
jsonbObjj.put("nombre", Integer.parseInt(no_of_addition
.getText().toString()));
jsonbObjj.put("cru", crue);
jsonbObjj.put("annee", 2010);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
VolleyRequest mVolleyRequest = new VolleyRequest(
Method.DELETE, url, jsonbObjj,
new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
// TODO Auto-generated method stub
if (pDialog != null) {
pDialog.dismiss();
}
Log.e("Server Response", "response = "
+ jsonObject.toString());
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
if (pDialog != null) {
pDialog.dismiss();
}
Log.e("Error Response",
"Error " + arg0.getMessage());
Log.e("Error Response",
"Error = " + arg0.getCause());
}
}, mUserSession.getUserEmail(), mUserSession
.getUserPassword(), false);
ApplicationController.getInstance().addToRequestQueue(
mVolleyRequest, "deleteRequest");
这是我的VolleyRequest请求类
and here is my VolleyRequest request class
public class VolleyRequest extends JsonObjectRequest {
String email, pass;
boolean saveCookeis;
public VolleyRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener,
String email, String pass, boolean saveCookie) {
super(method, url, jsonRequest, listener, errorListener);
// TODO Auto-generated constructor stub
this.email = email;
this.pass = pass;
this.saveCookeis = saveCookie;
}
public VolleyRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener) {
super(Method.POST, url, jsonRequest, listener, errorListener);
// TODO Auto-generated constructor stub
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
// TODO Auto-generated method stub
HashMap<String, String> params = new HashMap<String, String>();
String auth = "";
try {
auth = android.util.Base64.encodeToString(
(this.email + ":" + this.pass).getBytes("UTF-8"),
android.util.Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
params.put("Authorization", auth);
return params;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
// TODO Auto-generated method stub
if (saveCookeis) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
ApplicationController.getInstance().checkSessionCookie(
response.headers);
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
return super.parseNetworkResponse(response);
}
}
当我尝试此代码时,我得到400响应代码错误请让我知道是否有人可以帮助我..我在做什么错.谢谢
When i tried this code i get 400 response code error Please let me know if anyone can help me.. that what i am doing wrong. Thanks
这是我测试过的Delete Api的屏幕截图,并且工作正常.
here the screen shots for Delete Api which i tested and its working fine.
推荐答案
更新:
我已经将我的工作示例项目发布到 GitHub 来修复java.net.ProtocolException: DELETE does not support writing
,请看一下
I have posted my working sample project to GitHub to fix java.net.ProtocolException: DELETE does not support writing
, please take a look.
您的应用收到400
错误代码,因为尚未通过DELETE
请求发送数据主体.
Your app got 400
error code because the data body has not been sent with DELETE
request.
内部 HurlStack.java ,您将找到以下内容:
Inside HurlStack.java, you will find the following:
case Method.DELETE:
connection.setRequestMethod("DELETE");
break;
case Method.POST:
connection.setRequestMethod("POST");
addBodyIfExists(connection, request);
break;
因此我们可以看到DELETE
请求忽略正文数据.有一种解决方法,即您创建一个CustomHurlStack
类(复制上面HurlStack
的所有内容),仅作如下修改:
So we can see DELETE
request ignores body data. There's a workaround, that is you create a CustomHurlStack
class (copy all content of HurlStack
above), with only modification as the following:
case Request.Method.DELETE:
connection.setRequestMethod("DELETE");
addBodyIfExists(connection, request);
break;
然后在您的活动中致电:
Then, in your activity, call:
CustomHurlStack customHurlStack = new CustomHurlStack();
RequestQueue queue = Volley.newRequestQueue(this, customHurlStack);
请注意,此解决方法仅适用于API21 +(我尚未测试的API20).从API19-中,将抛出java.net.ProtocolException: DELETE does not support writing
.
Please note that this workaround works only for API21+ (API20 I have not tested). From API19-, java.net.ProtocolException: DELETE does not support writing
will be thrown.
P/S:如果您的应用程序compileSdkVersion 23
在build.gradle
文件中添加useLibrary 'org.apache.http.legacy'
,并且在创建CustomHurlStack
类时出现错误.
P/S: add useLibrary 'org.apache.http.legacy'
inside your build.gradle
file if your app compileSdkVersion 23
and you get error when create CustomHurlStack
class.
希望这会有所帮助!
这篇关于带标题和Parametes Volley的删除请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!