gpt4 book ai didi

java - Android HTTP GET 错误

转载 作者:可可西里 更新时间:2023-11-01 17:10:43 25 4
gpt4 key购买 nike

对不起,我是 java 和 android 的新手。我想了解为什么它总是响应错误。谢谢!

try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
//e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}

导入

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

是的,我进入了互联网许可

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testhttp"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

最佳答案

您似乎正试图在UI 线程 中发出http 请求。

将您的请求放在另一个线程中。

关于java - Android HTTP GET 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641513/

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