gpt4 book ai didi

android - 无法从 URL 获取 html 代码

转载 作者:行者123 更新时间:2023-11-30 00:14:55 24 4
gpt4 key购买 nike

请帮帮我。我正在尝试从 Android 中的 url 获取 html 代码,但代码有问题,我无法弄清楚。处理未超出此行,“HttpResponse response = httpclient.execute(httpget);”

我也尝试过其他方式,比如,

URLConnection connection = (new URL("https://google.com")).openConnection();
Scanner scanner = new Scanner(connection.getInputStream());<--Not executing

但我在这里也面临着同样的问题。

这是我的实际代码,

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://google.com");
HttpResponse response = httpclient.execute(httpget);<--This line is not executing
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");

String resString = sb.toString();

is.close();

这是我的日志,

11-22 22:58:29.233 5830-5830/com.example.sdp.smeme I/art: Not late-enabling -Xcheck:jni (already on)
11-22 22:58:31.610 5830-5830/com.example.sdp.smeme W/System: ClassLoader referenced unknown path: /data/app/com.example.sdp.smeme-1/lib/arm64
11-22 22:58:31.693 5830-5830/com.example.sdp.smeme I/InstantRun: starting instant run server: is main process
11-22 22:58:32.203 5830-5830/com.example.sdp.smeme W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-22 22:58:33.084 5830-5830/com.example.sdp.smeme D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-22 22:58:33.284 5830-5830/com.example.sdp.smeme W/gralloc_ranchu: Gralloc pipe failed

[ 11-
22 22:58:33.285 5830: 5830 D/ ]
HostConnection::get() New Host Connection established 0x78ce5e5900, tid 5830


[ 11-
22 22:58:33.545 5830: 5847 D/ ]
HostConnection::get() New Host Connection established 0x78ce5e59c0, tid 5847
11-22 22:58:33.552 5830-5847/com.example.sdp.smeme I/OpenGLRenderer: Initialized EGL, version 1.4
11-22 22:58:33.553 5830-5847/com.example.sdp.smeme D/OpenGLRenderer: Swap behavior 1
11-22 22:58:33.553 5830-5847/com.example.sdp.smeme W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
11-22 22:58:33.558 5830-5847/com.example.sdp.smeme D/OpenGLRenderer: Swap behavior 0
11-22 22:58:33.858 5830-5830/com.example.sdp.smeme W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

最佳答案

如果你想建立'https'连接,你可以使用这个:

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));

HttpParams params = new BasicHttpParams();

SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);

HttpClient client = new DefaultHttpClient(mgr, params);
HttpGet httpget = new HttpGet("https://google.com");

不要忘记在 list 文件中声明您的互联网权限:

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

编辑

您必须在后台线程上运行代码

关于android - 无法从 URL 获取 html 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47441089/

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