- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是一名 QA,希望了解更多有关 Java 编程的知识,我遇到的问题是:我正在尝试将员工数据发布到一些假 Rest API 的数据库中,但我得到了
Cannot write to a URLConnection if doOutput=false - call setDoOutput(true)"
到目前为止,我尝试了 StackOverflow 中的一些想法,但由于我缺乏经验,我很容易陷入更深的问题。所以网址是:http://dummy.restapiexample.com/api/v1/create首先我创建了一个 json 对象的 Employee 类:
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
Employees em = new Employees();
em.setEmployeeName("Alex");
em.setEmployeeSalary("1234");
em.setEmployeeAge("28");
try{
URL url = new URL("http://dummy.restapiexample.com/api/v1/create");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Unsuccessful call: HTTP error : "
+ conn.getResponseCode());
}
// URLConnection urlc = url.openConnection();
// urlc.setDoOutput(true);
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.print(new Gson().toJson(em));
pw.close();
pw.flush();
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream())
);
String json = "";
String output;
while ((output = br.readLine()) != null) {
json += output;
}
conn.disconnect();
System.out.println("Employee name: " + em.getEmployeeName());
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
好吧,使用您的一个想法并添加下一行代码(在上面的代码中进行了注释):
URLConnection urlc = url.openConnection();
urlc.setDoOutput(true);
所以代码如下:
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
Employees em = new Employees();
em.setEmployeeName("Alex");
em.setEmployeeSalary("1234");
em.setEmployeeAge("28");
try{
URL url = new URL("http://dummy.restapiexample.com/api/v1/create");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Unsuccessful call: HTTP error : "
+ conn.getResponseCode());
}
URLConnection urlc = url.openConnection();
urlc.setDoOutput(true);
PrintWriter pw = new PrintWriter(urlc.getOutputStream());
pw.print(new Gson().toJson(em));
pw.close();
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(
(urlc.getInputStream())));
String json = "";
String output;
while ((output = br.readLine()) != null) {
json += output;
}
conn.disconnect();
System.out.println("Employee name: " + em.getEmployeeName());
}
catch (MalformedURLException e) {
e.printStackTrace(); }
catch (IOException e) {
e.printStackTrace();
}
}}
使用第二个代码,我没有收到该错误,但没有插入数据库(使用 postman 使用 GET 方法检查)...
嗯,我错过了什么?我想,我错过了一些基本的东西......
最佳答案
使用 url.openConnection 两次意味着您获得两个不同的连接。您将请求发送到第二个连接,并尝试从第一个连接读取响应。您应该在最初打开的连接上调用 doOutput。
第二个问题是您在发送请求之前调用 getResponseCode。在http中,请求必须在服务器发送响应之前完全发送。您应该将调用 doOutput 并写入请求正文的代码移到尝试检查响应代码的代码之前。
关于Java:如果 doOutput=false,则无法写入 URLConnection - 调用 setDoOutput(true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52029598/
在我的程序中,我需要跟踪已打开的与某些 HTTP 服务器的连接列表 - 以便在需要时立即断开连接。 我遇到了以下问题。如果我连接到 HTTP 服务器,一切正常,但如果连接到 HTTPS,则连接不会从列
我正在尝试写信给 URLConnection#getOutputStream ,但是,在我调用 URLConnection#getInputStream 之前,实际上并没有发送任何数据。 .即使我设置
我有一组对象,我试图通过 POST 发送到 API。第一个对象将按原样通过,然后我得到: java.net.ProtocolException: cannot write request body a
我在尝试发送文本时遇到 URLConnection 编码问题。 我的代码是这样的: final URL url = new URL(urlString); final URLConnection ur
我有这种方法,可以从雅虎财经下载 .csv 文件并将其保存在本地。它是在循环期间访问的,因此它从列表中下载许多文件。然而,有时符号输入不正确、不再存在或连接超时。如何修改此方法,以便重试连接超时并跳过
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import
打开 URLConnection 时,我使用以下代码来获取内容长度,但它返回 -1。 URL url = new URL(sUrl[0]); URLConnection connection = ur
默认情况下,URLConnection 的超时为 0,这是无限制的。 XXXXX 的合理值是多少? URL url = ... URLConnection uCon = url.openConnect
我无法打开具有特定网络资源的 URLConnection。我得到了 “java.net.ConnectException:连接超时:”。是因为该域阻止了直接 URL 连接吗?如果是这样,他们是如何阻止
我看过文本文件示例,但是否以与 URLConnection 相同的方式将音频文件直接保存到服务器? 最佳答案 是的,一样的。尽管确保使用二进制输出流将内容写入磁盘。 类似于: URLConne
我一直在尝试从网页获取信息,特别是此网站:http://www.ncbi.nlm.nih.gov/pubmed?term=%22pulmonary%20disease%2C%20chronic%20o
在我使用 Apache 库 (org.apach.httpclient) 向带有参数 (BasicNameValuePair) 的 Php 脚本发出请求之前, 然后现在我想删除那些库以减小 APK 大
我正在使用 android 应用程序,我正在从 url 下载文件。一切正常,但是当互联网连接介于两者之间(打开连接后)时,下载超时永远不会发生并且连接永远不会结束。 给我一个解决这个问题的方案
我只是想了解一下 JacksonJson 库。为此,我尝试将 Places API 中的 JSON 数据转换为字符串。 我的 key 有效(我在浏览器和另一个应用程序中进行了测试),但出现错误。这是代
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用 curl
HttpsURLConnection 有问题 - 未使用代理。 这是代码: //proxy String type = "https"; System.getProperties().put(type
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写一个 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用cur
在我的应用程序中,我需要下载一些网页。我是这样做的 URL url = new URL(myUrl); HttpURLConnection conn = (HttpURLConnection) url
我正在尝试使用 URLConnection 获得最低级别的字节计数.我已经用 CountingInputStream 计算了两个流传递的数据和 CountingOutputStream来自 Apach
我正在使用这段代码创建一个常规的 HTTP 连接: URLConnection cn = new URL( "http://...." ).openConnection(); cn.connect()
我是一名优秀的程序员,十分优秀!