Android 4.0上的HttpURLConnection失败

Android 4.0上的HttpURLConnection失败,android,httpurlconnection,Android,Httpurlconnection,我正在使用此代码从网上获取我的变更日志 InputStream content = null; try { URL url = new URL("http://dreamhawk.blinkenshell.org/changelog.txt"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

我正在使用此代码从网上获取我的变更日志

InputStream content = null;
           try {


               URL url = new URL("http://dreamhawk.blinkenshell.org/changelog.txt");
               HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
               urlConnection.setRequestMethod("GET");
               urlConnection.connect();

               content = urlConnection.getInputStream();


               BufferedReader r = new BufferedReader(new InputStreamReader(content));
               StringBuilder total = new StringBuilder();
               String line;
               String NL = System.getProperty("line.separator");
               try {
                   while ((line = r.readLine()) != null) {
                       total.append(line + NL);
                   }
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }

               String page = total.toString();

               Dialog dialog = new Dialog(Main.this);

               dialog.setContentView(R.layout.custom_dialog);
               dialog.setTitle(R.string.changelog);
               dialog.setCanceledOnTouchOutside(true);

               TextView text = (TextView) dialog.findViewById(R.id.text);
               text.setTextSize(13);
               text.setText(page);
               dialog.show();
           } catch (Exception e) {
               //handle the exception !
           }
这在安卓2.3及以下版本上有效。。。但是自从ICS更新后,我什么也没有得到!
没有对话,没有回应,什么都没有!救命啊/

我知道在android 3.0中,主线程上的网络连接是不允许的

StrictMode自动打开。 我相信4.0版也一样

要解决此问题,必须在单独的线程上执行网络连接。。。例如,使用
异步任务


阅读这篇关于这个主题的博客文章:


那你为什么不使用网络客户端呢?(用法:),您实际上不需要处理StrictMode线程策略。如果谷歌认为改变它很重要,我又有谁会不同意呢

绝对美味!现在,我的变更日志显示为一个对话框,它应该。。。谷歌是怎么做到这一点的?原因是什么?再次感谢你!是 啊不久前,当我遇到同样的问题时,我有些挠头。我猜谷歌称之为建立一个更“安全”的开发过程真奇怪:/。但我。。。好吧,我还没有搜索过很多关于android 3.0和使用网络服务的信息。但这是我的下一个线索,启动一个3.0模拟器并在那里尝试。。。有点让你生气,它只在一个版本上工作,但在“最新版本”上不起作用:/请不要只是禁用严格模式。严格模式告诉你一些事情。在UI线程上进行网络呼叫将导致应用程序和整个手机冻结。在异步任务中进行网络调用这样的回答对新的Android开发人员来说非常危险。他们看到它有很多选票,并认为它是正确的。最初的答案(简单地关闭严格模式)是这个问题最糟糕的解决方案。。。我对答案进行了编辑,以指定应该做什么(如上面Janusz所述)。