Sending JSON to the server

I run the following Java, a PUT request HttpURLConnection PUT with JSON data to be sent from an Android device. After that, I will handle any exceptions that arise. GET is working fine.

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 get an HTTP 500 error (internal error code) that an unexpected property blocks the request. JSONArray contains JSONObjects, whose keys I know are correct. The server is pretty standard and expects HTTP PUT with JSON bodies.

Did I miss something?

+3
source share
1 answer

, Volley, API Google, HTTP (), (PUT, DELETE, POST, GET). : Google I/O ( ):

, , !

. ya!

0

Source: https://habr.com/ru/post/1785338/


All Articles