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.

4
  • what line is throwing the exception? Commented Oct 18, 2012 at 20:35
  • What are the android version you use Commented Oct 18, 2012 at 20:37
  • Are you still having this problem? Or is that part of this question? Or...? Commented Oct 18, 2012 at 20:39
  • Updated description... still having the problem I am afraid! Commented Oct 18, 2012 at 20:44

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.