下载HTML源代码的Android? [英] download html source android?
问题描述
我想下载一个网站源$ C $ C,并在文本框中显示,但我似乎得到一个错误,无法弄明白:■
I'm trying to download a website source code and display it in a textbox but I seem to get an error and can't figure it out :s
public void getHtml() throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.spartanjava.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
Toast.makeText(activity.this, line.toString(), Toast.LENGTH_LONG).show();
}
}
怎么来的,这并不工作,并抛出IOException?
how come this doesn't work and throw an IOException?
推荐答案
我觉得你可能缺少INTERNET权限在你的manifest.xml
注意<使用-许可>在下面的code提供
标记。我在Eclipse中测试了code,和它的作品。
I think you're probably missing INTERNET permission in your manifest.xml
Pay attention to <uses-permission>
tag provided in the code below. I tested your code in eclipse, and it works.
顺便说一句我认为使用字符串结果
以这种方式不会工作。没有测试那么远虽然。但我认为你不能只是添加字符串到字符串。你需要使用的StringBuilder
并追加新的字符串。
BTW I think using String result
in this way wont work. Didn't test that far though. But I think you cannot just add string to a string. You need to use stringBuilder
and append new strings.
编辑:测试此字符串结果
梅托德,和它的作品。也许问题是,你正在试图抛出这么多土司的一次。您的code抛出举杯为retrived HTML code每一行。我把你的 getHtml()
法键入字符串,并返回结果
,和它返回它正常...我想不出任何其他理由例外,除了在你的AndroidManifest.xml中缺少INTERNET权限....
tested this String result
metod, and it works. Maybe the problem is that you are trying to throw so many toast all at once. Your code throws a toast for every line of retrived html code. I set your getHtml()
method to type String, and to return result
, and it returned it properly... I can't think of any other reason for exception, except missing INTERNET permission in your AndroidManifest.xml....
干杯!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.test.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".test"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
这篇关于下载HTML源代码的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!