【问题标题】:HttpDelete a parameter with setEntity on android?在android上使用setEntity HttpDelete一个参数?
【发布时间】:2015-11-25 08:16:56
【问题描述】:

我尝试用这个删除一个参数:

private class SendfeedbackDeleteStudio extends AsyncTask<String, Void, String> {
    private static final String LOG_TAG = "DeleteStudio";
    Bundle extras = getIntent().getExtras();
    final String token= extras.getString("TOKEN");
    @Override
    protected String doInBackground(String... params) {
        String venid = params[0];
        Utils.log("venid: " + venid);
        final String url_delete_studio = Constant.URI_BASE_FAVOURITE;
        String contentType;
        contentType = "application/x-www-form-urlencoded";
        // do above Server call here
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("vendor_id", venid));
        try
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpDelete httpDelete = new HttpDelete(url_delete_studio);
            httpDelete.setHeader("Content-Type", contentType);
            httpDelete.setHeader("Authorization", "Bearer " + token);
            httpDelete.setHeader("Accept", "application/json");
            httpDelete.setHeader("Accept-Charset", "utf-8");
            httpDelete.setEntity(new UrlEncodedFormEntity(nameValuePair));
            HttpResponse response = httpClient.execute(httpDelete);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // EntityUtils to get the reponse content
                String content =  EntityUtils.toString(entity);
                Utils.log("daftar content: " + content);
                JSONObject hasiljson = new JSONObject(content);
                Utils.log("hasiljson object: " + hasiljson);
                String success = hasiljson.getString("success");
                Utils.log("success: " + success);
            }
            // writing response to log
            Log.d("Http Response:", response.toString());
        }
        catch (Exception e)
        {
            Log.e(LOG_TAG, String.format("Error during delete: %s", e.getMessage()));
        }
        return "processing";
    }

    @Override
    protected void onPostExecute(String message) {
        //process message
        clickFavourites();
    }
}

但它在 httpDelete.setEntity(new UrlEncodedFormEntity(nameValuePair)); 上变红,它似乎无法识别我发送删除的参数。如何删除venid参数?

【问题讨论】:

  • 从哪里删除参数?
  • 来自数据库(mysql)

标签: java android http-delete


【解决方案1】:

HTTP DELETE 类似于 GET 变体,因此它不会接受任何输入。

如果您希望通过正文提供删除,您可能需要考虑使用 POST 到接受正文的位置。

或者你可以使用这个

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;

@NotThreadSafe
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(); }
}

引用自here

【讨论】:

  • 如何将它与我的 url 和参数一起使用?
  • 或者最好使用GET,在api的SQL中使用DELETE命令?
  • @carijawaban 你可以访问 api 的代码吗?如果是,我建议您将代码更改为接收 POST 而不是 DELETE,然后将实体放入 HTTP POST,然后在您的 API 代码中使用 SQL DELETE
  • 我没有访问api,但是我朋友有访问权限,我会问他是否要更改它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
相关资源
最近更新 更多