问题描述
我想从用户输入时,code我到目前为止是这样的一个网站在一个字符串的HTML源代码:
I'm trying to get the HTML source in a string from a web site that the user enters, the code I have so far looks like this:
public String getURLContent(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
return page;
}
catch (ClientProtocolException e)
{
e.printStackTrace();
return "";
}
catch (IOException e)
{
e.printStackTrace();
return "";
}
}
每当我尝试运行此,我打它根据文档的第二个catch(IOException异常),表示服务器未能给予有效回应...我像HTTP网站的测试这样的:\\ www.google .COM \\,所以他们绝对应该响应
Every time I try to run this I hit the second catch (IOException), which according to the documentation means the server failed to give a valid response... I am testing this with sites like "http:\www.google.com\", so they should definitely be responding
推荐答案
您code是好的。请务必粘贴完整的网站路径: HTTP:// WWW。 [页码]。 [域]
例如: http://www.google.com
和此许可添加到的AndroidManifest.xml
Your code is ok. Make sure you paste full website path : http://www. [page] . [domain]
eg.: http://www.google.com
And add this PERMISSION to AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
之前(如果是新项目):
just before (if it is new project):
<application android:label="@string/app_name">
完整的示例:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name">
<activity android:name="MyActivity"
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>
</manifest>
这篇关于从Android应用程序读取HTML源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!