我是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.NetworkOnMainThreadException
12-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.access$600(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.NetworkOnMainThreadException
12-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
答案 0 :(得分:11)
您可以使用HttpClient来请求此信息。它将同步完成,但您也可以发出异步请求。
String myUri = "http://www.whatever.com";
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(myUri);
HttpResponse response = httpClient.execute(get);
// Build up result
String bodyHtml = EntityUtils.toString(response.getEntity());
您还需要将以下内容添加到应用的清单文件中。
<uses-permission android:name="android.permission.INTERNET" />
关于如何使用AsyncTask包装它的一个好主题是:Common class for AsyncTask in Android?
答案 1 :(得分:1)
第一个问题是您在请求页面时阻止了UI线程。您有可能在onCreate期间收到ANR。尝试使用AsyncTask来做这类事情。
另外,请确保您已在Manifest中声明了INTERNET权限。
请发布您的logcat输出,以便我们更好地了解问题。