" HttpDeleteWithBody"不从数据库中删除 [英] "HttpDeleteWithBody" isn't deleted from Database
问题描述
我想打电话给我的 API
发送 JSON
为的删除 从我的数据库产品;的但是,它不会删除任何东西。的
I'm trying to call to my API
sending a JSON
to delete a product from my DB; however, it doesn't delete anything.
的的 JSON
是响应真,它并没有给我任何错误;即便如此,当我做我的数据库查询,该产品仍然存在。的
The response of the JSON
is "true," and it doesn't give to me any error; even so, when I make a query on my DB, the product is still there.
我已经创建了一个名为类 HttpDeleteWithBody
,看起来像:
I've created a class called HttpDeleteWithBody
that looks like:
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() { return METHOD_NAME; }
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() { super(); }
}
然后在我的 doInBackGround
我的片段
,我这样做:
boolean resul = true;
try {
JSONObject usuari = new JSONObject();
try {
usuari.put("idProducte", params[0]);
usuari.put("idusuari", params[1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpEntity entity = new StringEntity(usuari.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpDeleteWithBody httpDeleteWithBody = new HttpDeleteWithBody(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari");
httpDeleteWithBody.setEntity(entity);
HttpResponse response = httpClient.execute(httpDeleteWithBody);
Log.d("Response ---------->", response.getStatusLine().toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception ex) {
Log.e("ServicioRest", "Error!", ex);
}
return resul;
此外,我试图做到这一点:
Furthermore, I've tried to do this:
HttpDeleteWithBody delete = new HttpDeleteWithBody(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari");
StringEntity se = new StringEntity(usuari.toString(), HTTP.UTF_8);
se.setContentType("application/json");
delete.setEntity(se);
但是,它不工作...日志说:
however, it doesn't work... the log says:
D /响应---------->:HTTP / 1.1 200 OK
D/Response ---------->﹕ HTTP/1.1 200 OK
这是我如何调用该方法:
This is how I call the method:
JSONObject deleteproduct = new JSONObject();
try {
deleteproduct.put("idProducte", String.valueOf(IDPROD));
deleteproduct.put("idusuari", String.valueOf(IDUSU));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Json test per afegir prod --> ", deleteproduct.toString());
TareaWSInsertar tarea = new TareaWSInsertar();
tarea.execute(String.valueOf(IDPROD), String.valueOf(IDUSU));
的我在我的谷歌浏览器添加一个插件叫做<一个href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm\"相对=nofollow>邮递员的,当我试图通过这种方式来做到这一点,它的正确删除...
I've added on my Google Chrome a plug-in called "PostMan" and when I try to do this by this way, it's deleting correctly...
我在做什么错了?
我试图用卷曲
,这是结果:
的它返回我假的,当我把同样的 JSON
为邮递员
;不过,如果我把同样的 JSON
在邮递员
,它工作正常。的
It is returning me false, when I put the same JSON
as PostMan
; nevertheless, if I put the same JSON
on PostMan
, it works fine.
我实现离子库我也喜欢它:
JSONObject usuari = new JSONObject();
try {
usuari.put("idProducte", params[0]);
usuari.put("idusuari", params[1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String url = getResources().getString(R.string.IPAPI) + "produsuaris/produsuari";
Log.d("CURL", "curl -X DELETE -d '" + usuari.toString() + "' " + url);
Builders.Any.F builder = Ion.with(getActivity().getApplicationContext())
.load(HttpDelete.METHOD_NAME, url)
.setTimeout(15000).setStringBody(usuari.toString());
String response = builder.toString();
Log.d("TEST", "Req response -->" + response);
}
catch (Exception ex){
resul = false;
}
和它仍然返回它的确定,不删除任何东西。
And it still returning that it's OK, and don't delete anything.
推荐答案
我终于解决了这个问题,我所做的是改变我的<$的 HttpDelete
方法C $ C API 和>,而不是发送一个 JSON
,我送参数(如 HTTPGET
),现在我的code是这样的:
Finally I solved the problem, what I've done is change the HttpDelete
method on my API
, and instead of send a JSON
, I send parameters (like a HttpGet
) and now my code is like :
boolean resul = true;
HttpClient httpClient = new DefaultHttpClient();
String id____USER = params[0];
String id____PROD = params[1];
HttpDelete del =
new HttpDelete(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari?idProd=" + Integer.parseInt(id____PROD)+"&idUs="+Integer.parseInt(id____USER));
del.setHeader("content-type", "application/json");
try
{
HttpResponse resp = httpClient.execute(del);
String respStr = EntityUtils.toString(resp.getEntity());
if(!respStr.equals("true"))
resul = false;
}
catch(Exception ex)
{
Log.e("ServicioRest","Error!", ex);
resul = false;
}
return resul;
这是我如何把这个 AsyncMethod
TareaWSInsertar tarea = new TareaWSInsertar();
tarea.execute(String.valueOf(IDUSU),String.valueOf(IDPROD));
这工作对我来说,我知道这是不是最好的解决办法,但我没有太多的时间,还我试过三种解决方案,并没有人没有工作。
This work to me, I know it's not the best solution, but I've no much time, also I tried three solutions and noone didn't work.
随意张贴一个正确的答案,如果你知道我在做什么错了。
Feel free to post a correct answer if you know what I was doing wrong.
这篇关于&QUOT; HttpDeleteWithBody&QUOT;不从数据库中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!