java–Android:将网页的HTML读入字符串

java–Android:将网页的HTML读入字符串,第1张

概述我是Android开发的新手,我正在尝试阅读网页的HTML并将其存储在下面的字符串(“myHTML”)中,并将其显示在应用程序上.但是,只要运行,应用程序就会结束.我一直在网上搜索这个原因,并且发现一些文章说由于其“昂贵”的性质,无法在应用程序的主UI线程中完成互联网访问.有没有人遇到过

我是Android开发的新手,我正在尝试阅读网页的HTML并将其存储在下面的字符串(“myHTML”)中,并将其显示在应用程序上.

但是,只要运行,应用程序就会结束.我一直在网上搜索这个原因,并且发现一些文章说由于其“昂贵”的性质,无法在应用程序的主UI线程中完成互联网访问.有没有人遇到过类似的问题?我会很感激有关这个问题的任何进一步信息…在初学者级别:)

这是程序:

import androID.app.Activity;import androID.os.Bundle;import androID.os.Handler;import androID.Widget.TextVIEw;import java.util.ArrayList;import java.util.regex.*;import java.net.*;import java.io.*;/*   * Gets A webpage's HTML and saves to a string   */public String WebPagetoHTML(String Webpage) throws   IOException{  URL x = new URL(Webpage);    BufferedReader in = new BufferedReader(          new inputStreamReader(          x.openStream()));    String y = "";    String inputline;    while ((inputline = in.readline()) != null)       y = y.concat(inputline);     in.close();  return y;     }public voID onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentVIEw(R.layout.main);      TextVIEw tv = new TextVIEw(this);       String FirstAddress = "http://www.Google.com";       String myHTML = "";  try {    myHTML = WebPagetoHTML(FirstAddress);  } catch (IOException e) {    e.printstacktrace();  }          tv.setText(myHTML);      setContentVIEw(tv);}

logcat的:

12-29 14:41:44.441: E/AndroIDRuntime(540): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.first.app/my.first.app.WhatHaveIMissedActivity}: androID.os.networkonmainthreadException12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1956)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.ActivityThread.access0(ActivityThread.java:123)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.os.Handler.dispatchMessage(Handler.java:99)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.os.Looper.loop(Looper.java:137)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.ActivityThread.main(ActivityThread.java:4424)12-29 14:41:44.441: E/AndroIDRuntime(540):  at java.lang.reflect.Method.invokeNative(Native Method)12-29 14:41:44.441: E/AndroIDRuntime(540):  at java.lang.reflect.Method.invoke(Method.java:511)12-29 14:41:44.441: E/AndroIDRuntime(540):  at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)12-29 14:41:44.441: E/AndroIDRuntime(540):  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)12-29 14:41:44.441: E/AndroIDRuntime(540):  at dalvik.system.NativeStart.main(Native Method)12-29 14:41:44.441: E/AndroIDRuntime(540): Caused by: androID.os.networkonmainthreadException12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.os.StrictMode$AndroIDBlockGuardPolicy.onNetwork(StrictMode.java:1099)12-29 14:41:44.441: E/AndroIDRuntime(540):  at java.net.InetAddress.lookupHostByname(InetAddress.java:391)12-29 14:41:44.441: E/AndroIDRuntime(540):  at java.net.InetAddress.getAllBynameImpl(InetAddress.java:242)12-29 14:41:44.441: E/AndroIDRuntime(540):  at java.net.InetAddress.getAllByname(InetAddress.java:220)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpconnection.<init>(httpconnection.java:71)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpconnection.<init>(httpconnection.java:50)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpconnection$Address.connect(httpconnection.java:351)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpconnectionPool.get(httpconnectionPool.java:86)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpconnection.connect(httpconnection.java:128)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpEngine.openSocketConnection(httpEngine.java:308)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpEngine.connect(httpEngine.java:303)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpEngine.sendSocketRequest(httpEngine.java:282)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpEngine.sendRequest(httpEngine.java:232)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpURLConnectionImpl.getResponse(httpURLConnectionImpl.java:273)12-29 14:41:44.441: E/AndroIDRuntime(540):  at libcore.net.http.httpURLConnectionImpl.getinputStream(httpURLConnectionImpl.java:168)12-29 14:41:44.441: E/AndroIDRuntime(540):  at java.net.URL.openStream(URL.java:462)12-29 14:41:44.441: E/AndroIDRuntime(540):  at my.first.app.WhatHaveIMissedActivity.WebPagetoHTML(WhatHaveIMissedActivity.java:71)12-29 14:41:44.441: E/AndroIDRuntime(540):  at my.first.app.WhatHaveIMissedActivity.onCreate(WhatHaveIMissedActivity.java:99)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.Activity.performCreate(Activity.java:4465)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)12-29 14:41:44.441: E/AndroIDRuntime(540):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1920)12-29 14:41:44.441: E/AndroIDRuntime(540):  ... 11 more

解决方法:

您可以使用httpClIEnt来请求此信息.它将同步完成,但您也可以发出异步请求.

String myUri = "http://www.whatever.com";httpClIEnt httpClIEnt = new DefaulthttpClIEnt();httpGet get = new httpGet(myUri);httpResponse response = httpClIEnt.execute(get);// Build up resultString bodyHTML = EntityUtils.toString(response.getEntity());

您还需要将以下内容添加到应用程序的清单文件中.

<uses-permission androID:name="androID.permission.INTERNET" />

关于如何使用AsyncTask包装它的一个很好的线程是:Common class for AsyncTask in Android?

总结

以上是内存溢出为你收集整理的java – Android:将网页的HTML读入字符串全部内容,希望文章能够帮你解决java – Android:将网页的HTML读入字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116553.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存