我正在运行以下 Java,这是一个 HttpURLConnection PUT 请求,其中包含将从 Android 设备发送的 JSON 数据。在此工作后,我将处理任何引发的异常。GET 工作得很好。
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(nameString, pwdString.toCharArray());
}
});
url = new URL(myURLString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestMethod("PUT");
urlConnection.setRequestProperty("Content-Type", "application/json");
OutputStream output = null;
try {
output = urlConnection.getOutputStream();
output.write(jsonArray.toString().getBytes());
} finally {
if (output != null) { output.close(); }
}
int status = ((HttpURLConnection) urlConnection).getResponseCode();
System.out.println("" + status);
urlConnection.disconnect();
我收到 HTTP 500 错误(内部错误代码),表示意外属性阻止了请求。JSONArray 包含我知道其键正确的 JSONObjects。该服务器非常标准,并且需要带有 JSON 主体的 HTTP PUT。
我错过了什么吗?