Since update to Ice Cream Sandwich, my POST request doesn't work anymore. Before ICS, this works fine:
try {
final URL url = new URL("http://example.com/api/user");
final HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(false);
connection.setDoInput(true);
connection.setRequestProperty("Content-Length", "0");
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.w(RestUploader.class.getSimpleName(), ": response code: " + connection.getResponseMessage());
} else {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
final String line = reader.readLine();
reader.close();
return Long.parseLong(line);
}
} catch (final MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (final ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
I've tried to set
connection.setDoOutput(true);
but it doesn't works. The server response is always a 405 (Method not allowed) and the server log says it was an GET request.
The Android JavaDoc to setRequestMethod says:
This method can only be called before the connection is made.
Does it mean that the method must be invoked before url.openConnection()? How should I create a HttpURLConnection instance? All the examples I've seen, make it as described above.
I hope someone has an idea why it always send a GET request instead of a POST.
Thanks in advance.
new AsyncTask<Void, Void, Boolean>() { @Override protected Boolean doInBackground(final Void... params) { //... }
– Flyweight