Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android应用程序中使用url获取html源代码_Android_Html_Url - Fatal编程技术网

在android应用程序中使用url获取html源代码

在android应用程序中使用url获取html源代码,android,html,url,Android,Html,Url,我试图编写一些代码,从用户那里获取url,然后单击submit按钮,我将获取url并进行调用,然后从页面检索html源代码。但是,我得到了以下例外情况: W/System.err(14858):android.os.NetworkOnMainThreadException W/System.err(14858):位于android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) 对于android 3.0

我试图编写一些代码,从用户那里获取url,然后单击submit按钮,我将获取url并进行调用,然后从页面检索html源代码。但是,我得到了以下例外情况:

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

对于android 3.0,我试图开发的平台似乎不允许我使用主方法上的网络资源。我知道有一些方法,比如在后台运行或者使用异步方法应该可以工作,但是有人可以指导我吗?我不太确定该怎么办。我是编程新手。 提前谢谢

下面是我当前在onclick方法上的代码:

    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);
    }

您可以使用可运行或线程,但最惯用的Android方法可能是使用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");
newasynctask(){
@凌驾
受保护的字符串doInBackground(字符串…urlStr){
//在非UI线程上执行任务
StringBuffer htmlCode=新的StringBuffer();
试一试{
URL=新URL(urlStr[0]);
BufferedReader in=新的BufferedReader(新的InputStreamReader(url.openStream());
字符串输入线;
而((inputLine=in.readLine())!=null){
htmlCode+=输入线;
Log.d(Log_标签,“html:+inputLine”);
}
in.close();
}捕获(例外e){
e、 printStackTrace();
Log.d(Log_标记,“Error:+e.getMessage());
Log.d(Log_标签,“HTML代码:”+htmlCode);
}
返回htmlCode.toString();
}         
@凌驾
受保护的void onPostExecute(字符串htmlCode){
//用html在UI线程上做一些事情
TextView out=(TextView)findViewById(R.id.out);
out.setText(htmlCode);
}
}.执行(”http://www.google.com");