Sending JSON to a server
Posted
by
SK9
on Stack Overflow
See other posts from Stack Overflow
or by SK9
Published on 2011-01-14T03:26:42Z
Indexed on
2011/01/15
5:53 UTC
Read the original article
Hit count: 262
I'm running the following Java, an HttpURLConnection PUT request with JSON data that will be sent from an Android device. I'll handle any raised exceptions after this is working.
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();
I'm receiving an HTTP 500 error (internal error code), that an unexpected property is blocking the request. The JSONArray comprises JSONObjects whose keys I know are correct. The server is pretty standard, and expects HTTP PUTs with JSON bodies.
Am I missing something glaring?
Thanking you kindly in advance.
© Stack Overflow or respective owner