下载HTML源$ C ​​$ c和加载它 [英] Downloading html source code and loading it

查看:218
本文介绍了下载HTML源$ C ​​$ c和加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个code从这里下载HTML源代码的Andr​​oid?。但是,当我尝试运行它,我的程序总是崩溃。我已经加入了互联网准许在,任何想法?

编辑:以下是完整的错误消息。 08-02 00:16:47.364:E / EmbeddedLogger(1577):错误获取包装标签:com.jimmyc.lawrenceh.schedulinglookup

EDIT2:它适用于Android 2.2,但它并没有在Android 4.0 / 3.0。

 私人无效的initialize(){
      //这里初始化变量
      尝试{
          getHtml();
      }
      赶上(IOException异常五){
          // TODO自动生成catch块
          e.printStackTrace();
      }
    }    公共无效getHtml()抛出ClientProtocolException,IOException异常{
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpContext的localContext =新BasicHttpContext();
        HTTPGET HTTPGET =新HTTPGET(http://www.yahoo.com);
        HTT presponse响应= httpClient.execute(HTTPGET,localContext);
        字符串结果=;        BufferedReader中读者=
            新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));        串线= NULL;
        而((行= reader.readLine())!= NULL){
            结果+ =行+\\ n;
            // Toast.makeText(Connect.this,line.toString(),Toast.LENGTH_LONG).show();        }
    }


解决方案

我想你想下载HTML code在UI线程。尝试下载它在后台运行,使用 AsynkTask

编辑。如果你说,它适用于Android 2.2,但它并没有在Android 4.0 / 3.0工作我完全确定您要下载它的UI线程。从Android 3.0的,你不能做长期的过程在UI线程因为你可以阻止它。你必须做的下载在不同的线程


PS。对不起,我的英语水平。

I found this code from here download html source android? . But when I try running it, my program keeps crashing. I have already added the internet permission in. Any ideas?

Edit: Here is the full error message. 08-02 00:16:47.364: E/EmbeddedLogger(1577): Error getting package label: com.jimmyc.lawrenceh.schedulinglookup

Edit2: It works on Android 2.2 but it doesn't work on Android 4.0/3.0.

 private void initialize() {
      //initialize variables here
      try {
          getHtml();
      }
      catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
    }

    public void getHtml() throws ClientProtocolException, IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://www.yahoo.com");
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";

        BufferedReader reader = 
            new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = null;
        while ((line = reader.readLine()) != null){
            result += line + "\n";
            // Toast.makeText(Connect.this, line.toString(), Toast.LENGTH_LONG).show();

        }
    }

解决方案

I think you are trying to download the HTML code in the UI Thread. Try to download it in the background, using AsynkTask

Edit. If you say that it works on Android 2.2 but it doesn't work on Android 4.0/3.0 I'm completely sure you are trying to download it in the UI thread. From Android 3.0, you can't do long process in the UI thread because you can block it. You must do the download in a different thread
PS. Sorry for my english.

这篇关于下载HTML源$ C ​​$ c和加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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