0

Working on the emulator, but when I am trying on an HTC Sense, i have got an exception on the last line of this code

url = new URL(urlString);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", 
                "application/json; charset=utf-8");

//Send request
Gson requestGson = new Gson();          
OutputStream wr = connection.getOutputStream();

The exception is java.net.ProtocolException: Does not support output for the connection.getOutputStream();

I am testing on API 10, Gingerbread on an HTC Sense (remote debugging).

anyone has encountered this problem before?

Thanks. David.

l_ingenu
  • 91
  • 1
  • 11

1 Answers1

1

Try calling setDoOutput(true). It is needed for POST requests. The first part of your code would change to look like this:

connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", 
            "application/json; charset=utf-8");
connection.setDoOutput(true);

Source: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/2aEYpsZEMvs

See also this other StackOverflow answer: What exactly does URLConnection.setDoOutput() affect?

Community
  • 1
  • 1
louielouie
  • 14,881
  • 3
  • 26
  • 31