在模拟器上工作,但是当我尝试使用HTC Sense时,我在此代码的最后一行有一个例外
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();
异常是java.net.ProtocolException:不支持connection.getOutputStream();
的输出我正在使用HTC Sense(远程调试)测试API 10,Gingerbread。
之前有人遇到过这个问题吗?
感谢。 大卫。
答案 0 :(得分:1)
尝试拨打setDoOutput(true)
。 POST请求需要它。代码的第一部分将更改为:
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
connection.setDoOutput(true);
来源:https://groups.google.com/forum/?fromgroups=#!topic/android-developers/2aEYpsZEMvs
另请参阅其他StackOverflow答案:What exactly does URLConnection.setDoOutput() affect?