3

我正在运行以下 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。

我错过了什么吗?

4

1 回答 1

0

首先,我真的建议你使用 Volley,这是一个专门为 HTTP(s) 请求开发的谷歌 API,它使用所有请求类型(PUT、DELETE、POST、GET)。我建议您观看此视频:来自 Google I/O 的视频 并遵循本教程(涵盖您的问题):教程链接

如果您决定尝试一下,请告诉我,我很乐意为您提供帮助!

再见 !

于 2013-10-31T05:48:40.913 回答