java - Android 中的 DELETE 请求无法连接到服务器

标签 java android

所以我的问题是如何在 Android Studio Java 中创建对 URL 的 DELETE 请求。我已经有一个从 URL 获取 json 的异步任务。所以我现在的问题是如何创建 DELETE 请求

编辑:

所以现在我得到了这段代码:

   int pos = arrlist.get(info.position).getId();
                    URL_DELETE = "http://testserver/test/tesst.php?id=" + pos + "&username=" + username + "&password=" + password;
                    URL url = null;
                    try {
                        url = new URL(URL_DELETE);
                        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
                        httpCon.setDoOutput(true);
                        httpCon.setRequestProperty(
                                "Content-Type", "application/x-www-form-urlencoded" );
                        httpCon.setRequestMethod("DELETE");

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

要了解给定 URL 的内容应该被删除。但如果我运行代码,什么也不会发生。

最佳答案

您需要调用connect()HttpURLConnection 上。现在您实际上并没有与服务器建立连接。

根据您对其他答案的评论,您还尝试在主(UI)线程上运行此代码 - 您需要将代码更改为 run on a background thread .

关于java - Android 中的 DELETE 请求无法连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60976278/

相关文章:

java - 使用数组进行冒泡排序 alg

java - 读取 Apache POI 文件并通过 JPA 进行持久化,但实体的持久化直到方法结束时才会发生

android - 无法在 iOS [Firebase] 中接收通知

android - 如何在android中实现加载更多recyclerview

java - 如何在 Visual Studio Code 中修改 Java 样板代码生成?

java - 如何从 Maven 项目导出接口(interface)作为新 Artifact

java - 如何在nuxeo插件中获取conversationId

java - 从 Android 中的 Openfire 服务器获取所有用户的列表

Android:CursorAdapter、ListView 和 CheckBox

android - 无法将硬编码的字符串值传递给 Kotlin 中的 @BindingAdapter