我有两个覆盆子pi将内容从同一个路由器上传到网页,偶尔会阻止彼此访问。为了在java中防止这种情况,你通常会使用线程并同步,等待/通知以及所有这些 - 但是当应用程序不知道彼此被删除时,你怎么能这样做呢? - 我的代码看起来像这样 - 并按预期工作。
public void sendStrings(String output) {
String url2 = "http://myhomepage.com";
HttpURLConnection connection = null;
try {
URL obj = new URL(url2);
connection = (HttpURLConnection) obj.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(output);
writer.flush();
writer.close();
InputStream input = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String otherLine;
int responseCode = connection.getResponseCode();
}
catch (IOException e) {
} finally {
connection.disconnect();
}
}