Android:java.net.protocolException不支持输出

前端之家收集整理的这篇文章主要介绍了Android:java.net.protocolException不支持输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我以前使用HttpClient和BasicNameValuePairs,由于某种原因我必须转移到HttpUrlConnection.

因此这个代码,使一个HttpPost请求具有一些参数:

  1. public class MConnections {
  2.  
  3. static String BaseURL = "http://www.xxxxxxxxx.com";
  4. static String charset = "UTF-8";
  5. private static String result;
  6. private static StringBuilder sb;
  7. private static List<String> cookies = new ArrayList<String>();
  8.  
  9. public static String PostData(String url,String sa[][]) {
  10.  
  11. HttpURLConnection connection = null;
  12. try {
  13. connection = (HttpURLConnection) new URL(BaseURL + url)
  14. .openConnection();
  15. } catch (MalformedURLException e1) {
  16. } catch (IOException e1) {
  17. }
  18.  
  19. cookies = connection.getHeaderFields().get("Set-Cookie");
  20. try{
  21. connection.setDoOutput(true); // Triggers POST.
  22. connection.setRequestProperty("Accept-Charset",charset);
  23. connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=" + charset);
  24. }catch (Exception e) {
  25.  
  26. //Here i get Exception that "java.lang.IllegalStateException: Already connected"
  27. }
  28. OutputStream output = null;
  29. String query = "";
  30. int n = sa.length;
  31. for (int i = 0; i < n; i++) {
  32. try {
  33. query = query + sa[i][0] + "="
  34. + URLEncoder.encode(sa[i][1],"UTF-8");
  35. } catch (UnsupportedEncodingException e) {
  36. }
  37. }
  38. try {
  39. output = connection.getOutputStream();
  40. output.write(query.getBytes(charset));
  41. } catch (Exception e) {
  42.  
  43. //Here i get Exception that "android: java.net.protocolException: Does not support output"
  44. } finally {
  45.  
  46. if (output != null)
  47. try {
  48. output.close();
  49. } catch (IOException e) {
  50. }
  51. }
  52. InputStream response = null;
  53. try {
  54. response = connection.getInputStream();
  55. } catch (IOException e) {
  56.  
  57. //Here i get Exception that "java.io.IOException: BufferedInputStream is closed"
  58. } finally {
  59.  
  60. //But i am closing it here
  61. connection.disconnect();
  62. }
  63. try {
  64. BufferedReader reader = new BufferedReader(new InputStreamReader(
  65. response,"iso-8859-1"),8);
  66. sb = new StringBuilder();
  67. sb.append(reader.readLine());
  68. String line = "0";
  69. while ((line = reader.readLine()) != null) {
  70. sb.append("\n" + line);
  71. }
  72. response.close();
  73. result = sb.toString();
  74. } catch (Exception e) {
  75. }
  76. return result;
  77. }
  78. }

但是我在代码中注释了这样的异常.

其实我正在使用AsyncTask从我的Activity中调用MConnections.PostData()两次.这可能会导致异常:已连接,但我正在使用connection.disconnect.但为什么我还会得到这个异常?

我用错了吗?

谢谢

解决方法

对于协议异常,请在调用getOutputStream()之前尝试添加以下内容
  1. connection.setDoOutput(true);

发现这个答案感谢Brian Roach在这里的答复:https://stackoverflow.com/a/14026377/387781

旁注:我在运行Gingerbread的HTC Thunderbolt上出现了这个问题,但不是在运行Jelly Bean的Nexus 4上.

猜你在找的Android相关文章