Android:从网页获取 html 时遇到问题
我正在编写一个 android 应用程序,该应用程序应该从 php 页面获取 html 并使用从页面解析的数据。我在这里搜索了这个问题,最终使用了另一张海报上的示例中的一些代码。到目前为止,这是我的代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
Log.d("first","first");
HttpResponse response = client.execute(request);
String html = "";
Log.d("second","second");
InputStream in = response.getEntity().getContent();
Log.d("third","third");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Log.d("fourth","fourth");
StringBuilder str = new StringBuilder();
String line = null;
Log.d("fifth","fifth");
while((line = reader.readLine()) != null) {
Log.d("request line",line);
}
in.close();
} catch (ClientProtocolException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("error", "error");
}
Log.d("end","end");
}
就像我之前所说的,该 url 是一个 php 页面。每当我运行此代码时,它都会打印出第一条消息,然后打印出错误消息,最后打印出结束消息。我尝试过修改标题,但没有成功。任何帮助将不胜感激,因为我不知道我做错了什么。
谢谢!
当我执行 e.getMessage() 并将其打印出来时,记录器中显示的全部内容是 stanford.edu。希望有帮助。这是堆栈跟踪:
01-17 16:58:27.687: WARN/System.err(452): java.net.UnknownHostException: stanford.edu 01-17 16:58:27.947: WARN/System.err(452): 在 java.net.InetAddress.lookupHostByName(InetAddress.java:506) 01-17 16:58:27.947:警告/System.err(452):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 01-17 16:58:27.977: WARN/System.err(452): 在 java.net.InetAddress.getAllByName(InetAddress.java:256) 01-17 16:58:27.977:警告/System.err(452):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136) 01-17 16:58:28.027: WARN/System.err(452): 在 org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 01-17 16:58:28.027:警告/System.err(452):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 01-17 16:58:28.047: WARN/System.err(452): 在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348) 01-17 16:58:28.057: WARN/System.err(452): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 01-17 16:58:28.057:警告/System.err(452):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 01-17 16:58:28.117: WARN/System.err(452): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 01-17 16:58:28.117:警告/System.err(452):在edu.stanford.cs247.util.ColorHandler.getInfo(ColorHandler.java:44) 01-17 16:58:28.217:警告/System.err(452):在edu.stanford.cs247.util.ColorHandler.handleMessage(ColorHandler.java:70) 01-17 16:58:28.217: WARN/System.err(452): 在 android.os.Handler.dispatchMessage(Handler.java:99) 01-17 16:58:28.247: WARN/System.err(452): 在 android.os.Looper.loop(Looper.java:123) 01-17 16:58:28.257:警告/System.err(452):在android.app.ActivityThread.main(ActivityThread.java:3647) 01-17 16:58:28.257: WARN/System.err(452): 在 java.lang.reflect.Method.invokeNative(本机方法) 01-17 16:58:28.297:警告/System.err(452):在java.lang.reflect.Method.invoke(Method.java:507) 01-17 16:58:28.297: WARN/System.err(452): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-17 16:58:28.327: WARN/System.err(452): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-17 16:58:28.337:WARN / System.err(452):在dalvik.system.NativeStart.main(本机方法)
I'm writing an android application that is supposed to get the html from a php page and use the parsed data from thepage. I've searched for this issue on here, and ended up using some code from an example another poster put up. Here is my code so far:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
Log.d("first","first");
HttpResponse response = client.execute(request);
String html = "";
Log.d("second","second");
InputStream in = response.getEntity().getContent();
Log.d("third","third");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Log.d("fourth","fourth");
StringBuilder str = new StringBuilder();
String line = null;
Log.d("fifth","fifth");
while((line = reader.readLine()) != null) {
Log.d("request line",line);
}
in.close();
} catch (ClientProtocolException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("error", "error");
}
Log.d("end","end");
}
Like I said before, the url is a php page. Whenever I run this code, it prints out the first first message, but then prints out the error error message and then finally the end end message. I've tried modifying the headers, but I've had no luck with it. Any help would be greatly appreciated as I don't know what I'm doing wrong.
Thanks!
When I do e.getMessage() and print that out, in the logger all it says is stanford.edu. Hope that helps. Here is the stack trace:
01-17 16:58:27.687: WARN/System.err(452): java.net.UnknownHostException: stanford.edu
01-17 16:58:27.947: WARN/System.err(452): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
01-17 16:58:27.947: WARN/System.err(452): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
01-17 16:58:27.977: WARN/System.err(452): at java.net.InetAddress.getAllByName(InetAddress.java:256)
01-17 16:58:27.977: WARN/System.err(452): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
01-17 16:58:28.027: WARN/System.err(452): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-17 16:58:28.027: WARN/System.err(452): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-17 16:58:28.047: WARN/System.err(452): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
01-17 16:58:28.057: WARN/System.err(452): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-17 16:58:28.057: WARN/System.err(452): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-17 16:58:28.117: WARN/System.err(452): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-17 16:58:28.117: WARN/System.err(452): at edu.stanford.cs247.util.ColorHandler.getInfo(ColorHandler.java:44)
01-17 16:58:28.217: WARN/System.err(452): at edu.stanford.cs247.util.ColorHandler.handleMessage(ColorHandler.java:70)
01-17 16:58:28.217: WARN/System.err(452): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 16:58:28.247: WARN/System.err(452): at android.os.Looper.loop(Looper.java:123)
01-17 16:58:28.257: WARN/System.err(452): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-17 16:58:28.257: WARN/System.err(452): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 16:58:28.297: WARN/System.err(452): at java.lang.reflect.Method.invoke(Method.java:507)
01-17 16:58:28.297: WARN/System.err(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-17 16:58:28.327: WARN/System.err(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-17 16:58:28.337: WARN/System.err(452): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,
Log.d()
方法调用可能类似于Log.d("MyAppName", "Message")
而不是Log.d( "Message", "Message")
通过这种方式,您可以在 LogCat 中为您的应用创建一个过滤器,并且仅查看您的消息。 (在 Eclipse 上检查这一点)对于您的问题,请尝试在 catch 块中放入一些有用的消息,例如:
这将向您显示引发异常的问题。
考虑看看 此示例代码,
getUrlContent()
完全按照您的方式执行。祝你好运
First of all, the
Log.d()
method calls may be something likeLog.d("MyAppName", "Message")
notLog.d("Message", "Message")
In this way you can create a filter in LogCat for your app and see only your messages. (Check this on Eclipse)For your problem, try putting some useful message in the catch block, something like:
This will show you the problem that throw the exception.
Consider take a look to this sample code, the
getUrlContent()
do exactly what you wont.Good luck
堆栈跟踪的第一行显示:
你应该学会阅读堆栈跟踪。快速搜索“UnknownHostException”会告诉您它是:
这意味着它无法将 stanford.edu 转变为 IP。您可能需要
www.stanford.edu
或类似的东西。尝试在命令行上 ping 地址ping www.stanford.edu
,并确保它解析为 IP 地址。The first line of your stacktrace says:
You should learn to read stacktraces. A quick search for "UnknownHostException" tells you it is:
This means that it can't turn stanford.edu into an IP. You probably need
www.stanford.edu
or something like that. Try pinging the address on the command lineping www.stanford.edu
, and make sure it resolves to an ip address.我忘记在清单文件中添加互联网权限。
I forgot to add the internet permission in my manifest file.
添加到清单
add to Manifest
<uses-permission android:name="android.permission.INTERNET" />