【发布时间】:2015-04-29 15:48:38
【问题描述】:
我正在尝试实现HttpDelete,我使用了HttpGet 和HttpPost,并且运行良好。
首先想到,我在HttpDelete 上看到,我不能将del.setEntity(entity); 放在实体为StringEntity entity = new StringEntity(usuari.toString()); 的位置,而usuari 是JSON。
由于我无法将实体放在我的HttpDelete 上,我尝试了这个:
boolean resul = true;
HttpClient httpClient = new DefaultHttpClient();
HttpDelete del = new HttpDelete(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari");
del.setHeader("content-type", "application/json");
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();
}
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);
}
return resul;
我不知道如何将JSONObject usuari 放在我的HttpDelete 的实体上。我错过了什么或做错了什么?
【问题讨论】:
-
我不想听起来烦人,你为什么不尝试改用更完整的 http 客户端呢?使用Ion,您可以在 5,6 行代码中完成此类操作。
-
我已经用这个方法得到了所有的 POST 和 GET ......我不想改变我的代码,我认为使用 HttpDelete 是可能的
-
HttpDelete with body的可能重复