gpt4 book ai didi

java - Android http连接无法连接到本地主机可以连接到外部网站

转载 作者:行者123 更新时间:2023-11-29 05:07:00 25 4
gpt4 key购买 nike

我的应用程序中有这段代码,是我从 stackoverflow 复制并粘贴的。它所做的只是向网站发出一个 http 请求,并返回其内容(而且它运行完美)。

class RequestTask extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ShowMsg(thisone,result);
}
}

当我用它来获取网站的内容时,像这样:

new RequestTask().execute("http://www.stackoverflow.com");

一切都很好。它显示了网站的完整代码,一切都很好。但是当我尝试获取在我的 apache 服务器上运行的网站的内容时,它什么也没显示:

new RequestTask().execute("http://127.0.0.1");

为什么会这样?

最佳答案

android 的 Localhost 是 .... android 手机。您需要输入您的 apache 服务器的名称或 ip。

所以如果你运行的是本地模拟器,你应该输入你机器的IP。

关于java - Android http连接无法连接到本地主机可以连接到外部网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30064165/

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