获得html源代码code使用URL在Android应用程序 [英] getting the html source code with a url in an android application

查看:76
本文介绍了获得html源代码code使用URL在Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一些codeS,在一个url获取用户和后点击提交按钮的时候,我会的网址和拨打电话和检索HTML源代码code这一页。不过,我已经得到了以下的异常:

W / System.err的(14858):android.os.NetworkOnMainThreadException
W / System.err的(14858):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)

似乎为Android 3.0,我试图开发平台上不允许我使用的主要方法的网络资源。据我所知,有这样的背景,或者使用异步方法应该工作运行它的方法,但任何人都可以指导我吗?我不是如何去太肯定。我是新来编程。
先谢谢了。

下面是我目前的code,上的onclick方法:

 字符串的html code =;    尝试{
    网址URL =新的URL(http://www.google.com);
    在的BufferedReader =新的BufferedReader(新的InputStreamReader(url.openStream()));    串inputLine;    而((inputLine = in.readLine())!= NULL){
        HTML code + = inputLine;
        Log.d(LOG_TAG的HTML:+ inputLine);
    }    附寄();
    }赶上(例外五){
        e.printStackTrace();
        Log.d(LOG_TAG,错误:+ e.getMessage());
        Log.d(LOG_TAG,HTML code:+ HTML code);
    }


解决方案

您可以使用一个Runnable或线程,但可能是最地道的Andr​​oid方式做到这一点是使用AsyncTask的。

 新的AsyncTask<弦乐,太虚,字符串>(){
  @覆盖
  保护字符串doInBackground(字符串... urlStr){
    //做的东西对非UI线程
    StringBuffer的HTML code =新的StringBuffer();
    尝试{
      网址URL =新的URL(urlStr [0]);
      在的BufferedReader =新的BufferedReader(新的InputStreamReader(url.openStream()));      串inputLine;      而((inputLine = in.readLine())!= NULL){
        HTML code + = inputLine;
        Log.d(LOG_TAG的HTML:+ inputLine);
      }      附寄();
    }赶上(例外五){
        e.printStackTrace();
        Log.d(LOG_TAG,错误:+ e.getMessage());
        Log.d(LOG_TAG,HTML code:+ HTML code);
    }
    返回HTML code.toString();
  }  @覆盖
  保护无效onPostExecute(字符串HTML code){
    //做的东西在UI线程与HTML
    TextView的出=(的TextView)findViewById(R.id.out);
    out.setText(HTML code);
  }
} .execute(http://www.google.com);

I was trying to write some codes that takes in a url from the user and after which when the submit button is clicked, I will take the url and make a call and retrieve the html source code from the page. However, I have gotten a exception of the following:

W/System.err(14858): android.os.NetworkOnMainThreadException W/System.err(14858): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)

It seems that for android 3.0 the platform that I am trying to development on doesn't allow me to use the network resources on the main method. I understand that there are methods such as running it in the background or use the async method should work, but can anyone guide me on this? I'm not too sure on how to go about it. I'm new to programming. Thanks in advance.

Below is my current code, on the onclick method:

    String htmlCode = ""; 

    try {
    URL url = new URL("http://www.google.com");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        htmlCode += inputLine;
        Log.d(LOG_TAG, "html: " + inputLine);
    }

    in.close();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(LOG_TAG, "Error: " + e.getMessage());
        Log.d(LOG_TAG, "HTML CODE: " + htmlCode);
    }

解决方案

You could use a Runnable or Thread, but probably the most idiomatic Android way to do this is to use an AsyncTask.

new AsyncTask<String, Void, String>(){
  @Override
  protected String doInBackground(String... urlStr){
    // do stuff on non-UI thread
    StringBuffer htmlCode = new StringBuffer();
    try{
      URL url = new URL(urlStr[0]);
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

      String inputLine;

      while ((inputLine = in.readLine()) != null) {
        htmlCode += inputLine;
        Log.d(LOG_TAG, "html: " + inputLine);
      }

      in.close();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(LOG_TAG, "Error: " + e.getMessage());
        Log.d(LOG_TAG, "HTML CODE: " + htmlCode);
    }
    return htmlCode.toString();
  }         

  @Override
  protected void onPostExecute(String htmlCode){
    // do stuff on UI thread with the html
    TextView out = (TextView) findViewById(R.id.out);
    out.setText(htmlCode);
  }
}.execute("http://www.google.com");

这篇关于获得html源代码code使用URL在Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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