2011-12-05 59 views
1

我使用的代碼檢索並顯示更改日誌,如果它的第一次啓動,那麼簡單:)。Android HttpGet不適用於Android 4.0?

這非常適用於Android 2.3及較低,但自ICS-ROM上和模擬器嘗試時,失敗......

if(lastVersion < currentVersion) { 

      InputStream content = null; 
      try { 
       HttpGet httpGet = new HttpGet("http://dreamhawk.blinkenshell.org/changelog.txt"); 
       httpGet.setHeader("Content-Type", "text/plain; charset=utf-8"); 
       HttpClient httpclient = new DefaultHttpClient(); 
       // Execute HTTP Get Request 
       HttpResponse response = httpclient.execute(httpGet); 
       content = response.getEntity().getContent(); 


      } catch (Exception e) { 
       //handle the exception ! 
      } 
      if(content == null) { 
       lastVersion = Prefs.getInt("firstRun", -1); 
       Toast.makeText(Main.this, 
         "Failed fetching changelog on first run, connected to Internet?", 
          Toast.LENGTH_SHORT).show(); 

      } else { 
      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(); 
      } 
     }  

顯然,內容爲空的,我不知道爲什麼..因爲它的工作在其他Android版本,我不知道什麼可能是錯誤的...但作爲我的小白,我可能使用一些不好的代碼...

小心幫忙嗎? :)

+0

您的模擬器是否需要設置代理才能訪問Internet? –

+0

不,它不: - \ – DreamHawk

回答

0

考慮如果問題可能與this有關? 您不能在蜂窩(3.0)的主線程上執行聯網操作並轉發。

相關問題