Android HttpGet无法在Android 4.0上运行?

时间:2011-12-05 10:30:23

标签: android http-get

我正在使用一个代码来检索并显示Changelog,如果它是第一次启动,那么简单:)。

这在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版本上工作,我不知道什么可能是错的...但是作为我的菜鸟,我是可能使用了一些不好的代码...

关心帮忙? :)

2 个答案:

答案 0 :(得分:0)

尝试检查输入流的数据可用性,而不是!= null。 见答案:https://stackoverflow.com/a/1524309/222474

答案 1 :(得分:0)

如果问题可能与this有关,请考虑? 您无法在蜂窝(3.0)和转发主线程上执行网络操作。