Android 向服务器发送JSON

Android 向服务器发送JSON,android,json,httpurlconnection,Android,Json,Httpurlconnection,我正在运行以下Java,一个HttpURLConnection PUT请求,其中包含将从Android设备发送的JSON数据。我将在这项工作完成后处理任何引发的异常。GET工作得很好 Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAu

我正在运行以下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包含JSONObject,我知道它们的键是正确的。服务器是相当标准的,需要HTTP PUT和JSON主体


我遗漏了什么吗?

首先,我真的建议您使用Volley,这是一个专门为HTTP(s)请求开发的google API,使用所有请求类型(PUT、DELETE、POST、GET)。 我建议你看这个: 以下是本教程(涵盖您的问题):

如果你决定试一试,就告诉我,我很乐意帮助你


再见

谢谢你的建议。这个问题已经很老了,事情还在继续。我已经用截击或者毕加索了。