HttpURLConnection的在Android 4.0出现故障出现故障、HttpURLConnection、Android

2023-09-07 11:39:04 作者:不供祖宗

我用这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:

 
精彩推荐