gpt4 book ai didi

android - 从 Android 应用程序读取 HTML 源代码

转载 作者:可可西里 更新时间:2023-11-01 16:34:40 25 4
gpt4 key购买 nike

我正在尝试从用户输入的网站获取字符串中的 HTML 源代码,目前我的代码如下所示:

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 "";
}
}

每次我尝试运行它时,我都会遇到第二个问题(IOException),根据文档,这意味着服务器未能给出有效响应...我正在使用诸如“http:\www.google”之类的网站进行测试.com\",所以他们肯定会回应

最佳答案

您的代码没问题。确保粘贴完整的网站路径:http://www。 [页] 。 [域] 例如:http://www.google.com并将此权限添加到 AndroidManifest.xml

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

就在之前(如果是新项目):

<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 - 从 Android 应用程序读取 HTML 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com