Mus*_*bal 5 android sql-delete android-volley jsonobject
嗨我想使用标题和身体参数使用Volley向服务器发送删除请求.但我无法成功发送请求
我试过的
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");
Run Code Online (Sandbox Code Playgroud)
这是我的VolleyRequest请求类
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个代码我得到400响应代码错误请让我知道,如果有人可以帮助我...我做错了什么.谢谢
这里是我测试的删除Api的屏幕截图,它的工作正常.
更新:
我已将我的工作示例项目发布到GitHub进行修复java.net.ProtocolException: DELETE does not support writing,请查看.
您的应用获得了400错误代码,因为数据正文尚未随DELETE请求一起发送.
在HurlStack.java中,您将找到以下内容:
case Method.DELETE:
connection.setRequestMethod("DELETE");
break;
case Method.POST:
connection.setRequestMethod("POST");
addBodyIfExists(connection, request);
break;
Run Code Online (Sandbox Code Playgroud)
所以我们可以看到DELETE请求忽略了身体数据.有一个解决方法,就是你创建一个CustomHurlStack类(复制HurlStack上面的所有内容),只修改如下:
case Request.Method.DELETE:
connection.setRequestMethod("DELETE");
addBodyIfExists(connection, request);
break;
Run Code Online (Sandbox Code Playgroud)
然后,在您的活动中,致电:
CustomHurlStack customHurlStack = new CustomHurlStack();
RequestQueue queue = Volley.newRequestQueue(this, customHurlStack);
Run Code Online (Sandbox Code Playgroud)
请注意,此解决方法仅适用于API21 +(API20我尚未测试).从API19-,java.net.ProtocolException: DELETE does not support writing将被抛出.
P/S:如果您的应用程序useLibrary 'org.apache.http.legacy'在build.gradle文件中添加,则compileSdkVersion 23在创建CustomHurlStack类时会出现错误.
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
7367 次 |
| 最近记录: |