gpt4 book ai didi

java - HttpDelete 参数与 setEntity 在 android 上?

转载 作者:行者123 更新时间:2023-11-30 10:53:43 24 4
gpt4 key购买 nike

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

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)); 上变红了,它似乎无法识别我发送给 delete 的参数。如何删除venid参数?

最佳答案

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

关于java - HttpDelete 参数与 setEntity 在 android 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33911438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com