HttpURLConnection的在Android 4.0出现故障 [英] HttpURLConnection fails on Android 4.0

查看:223
本文介绍了HttpURLConnection的在Android 4.0出现故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code来从网上我的changelog。

I am using this code to fetch my changelog from the net.

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 !
           }

这工作,在Android 2.3及以下......不过既然ICS更新,我得到什么! 没有对话,没有任何反应,什么都没有!帮帮我! :/

This works, on Android 2.3 and below... But since the ICS update, i am getting nothing! No dialog, no response, no nothing! Help! :/

推荐答案

我知道,在允许的主线程未启用的Andr​​oid 3.0的网络连接。

I know in android 3.0 network connection on the main thread isnt permitted.

StrictMode自动开启。 我敢肯定它是相同的4.0

StrictMode is turned on automatically. Im sure it is the same for 4.0

要解决这个问题,你的必须的执行在一个单独的线程的网络连接......例如,使用的AsyncTask

To fix the issue, you must perform the network connection on a separate thread... for example, using an AsyncTask.

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

Read this blog post on the subject:

这篇关于HttpURLConnection的在Android 4.0出现故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆