1

I am using AsyncTask for REST WEB Service. I need to use HttpDelete method with Json Object to delete records. When i am implementing setEntity with HttpDelete it show's error on "setEntity". This is structure of my body:

{
"product_id": "80"
}

This is the Code to create this structure.

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

This is my complete AsyncTask Http call

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();

            }
        }
    }

please suggest how to set body in HttpDelete call.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

0 Answers0