如何在android中为HttpDelete调用设置body(setEntity)?

时间:2017-07-31 06:24:02

标签: android android-asynctask

我正在AsyncTask使用REST WEB Service。我需要使用HttpDelete方法和Json对象来删除记录。当我使用setEntity实施HttpDelete时,它会在"setEntity"上显示错误。 这是我身体的结构:

{
"product_id": "80"
}

这是创建此结构的代码。

JSONObject jsonObject = new JSONObject();
jsonObject.put("product_id", txt1);
request.setEntity(new StringEntity(jsonObject.toString(), "UTF-8")); 

这是我完整的AsyncTask Http电话

public class AsynDeleteWishlist extends AsyncTask<Void, Void, AsyncTaskResult<Object>> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            findViewById(R.id.rlProgress).setVisibility(View.VISIBLE);
        }

        @Override
        protected AsyncTaskResult<Object> doInBackground(Void... params) {
            BufferedReader in = null;
            try {

                HttpParams httpParameters = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
                HttpConnectionParams.setSoTimeout(httpParameters, 20000);

                HttpClient client = new DefaultHttpClient(httpParameters);

                String url1 = "Myurl/api/tag/172";
                HttpDelete request = new HttpDelete(url1);

                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");
                request.setHeader("Authorization", "Basic " + "token");

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("product_id", txt1);
                request.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));//setEntity shows error
                HttpResponse response = client.execute(request);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();

                String result1 = sb.toString();
                ArrayList<String> limits = new ArrayList<String>();

                limits.add(result1);

                final String[] res = limits.toArray(new String[limits.size()]);

                return new AsyncTaskResult<Object>(result1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return new AsyncTaskResult<Object>("");
        }

        @Override
        protected void onPostExecute(AsyncTaskResult<Object> result1) {
            super.onPostExecute(result1);
                if (result1.getError() != null) {
                String response = result1.getResult().toString();

            }
        }
    }

请建议如何在HttpDelete电话中设置正文。

0 个答案:

没有答案