gpt4 book ai didi

android - HTTP请求json-android中出现未知主机异常

转载 作者:行者123 更新时间:2023-11-29 00:49:54 24 4
gpt4 key购买 nike

我真的不知道为什么这个 android 代码不工作。它显示未知主机异常:

String requestTpServer = "www.google.com";
HttpClient client = new DefaultHttpClient();
HttpResponse response = null ;

try {

HttpGet get = new HttpGet(requestToServer);

get.setHeader("Accept", "application/json");
get.setHeader("content-type", "application/json");

response = client.execute(getm);
Log.i("Hit Success... in get deals response", "going to rendernow");

if (response != null ) {
Log.i("responsecode---", ""+response.getStatusLine().getStatusCode());
String jsonResponseFromServer = response.toString();
Log.i("json Response From Server", jsonResponseFromServer);
}
else {
Log.i("blank ", "reply ");
}
} catch (ClientProtocolException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

String res = response.toString();
return res;

最佳答案

出于好奇:您是否尝试在您的 URL 前加上“http://”前缀?

我正在使用以下助手打开我的 HTTP 连接:

private InputStream openHttpConnection() throws IOException {
InputStream in = null;
int response = -1;

URL url = new URL("http://www.myserver.com/someurl");
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}

也许这有帮助。

关于android - HTTP请求json-android中出现未知主机异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3698365/

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