android delete方法,在Android中发送HTTP DELETE请求
时间: 2024-04-01 21:37:40 浏览: 28
在 Android 中发送 HTTP DELETE 请求,可以使用 HttpURLConnection 或者 OkHttp 这样的网络库来完成。
下面是使用 HttpURLConnection 发送 DELETE 请求的示例代码:
```java
URL url = new URL("http://example.com/delete_endpoint");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("DELETE");
int responseCode = conn.getResponseCode();
```
这里我们使用了 Java 中的 HttpURLConnection 类来创建 HTTP 连接,并设置请求方法为 DELETE,然后发送请求并获取响应的状态码。
同样的,使用 OkHttp 发送 DELETE 请求的示例代码如下:
```java
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://example.com/delete_endpoint")
.delete()
.build();
Response response = client.newCall(request).execute();
```
这里我们使用了 OkHttp 的 OkHttpClient 类来创建 HTTP 客户端,然后创建一个 DELETE 请求,发送请求并获取响应。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)