错误连接被拒绝 [英] error connection refused
本文介绍了错误连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要建立HTTP连接到我自己的servlet。这是我的code:
I want to make an Http Connection to my own servlet. Here is my code:
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httpMethod = new HttpPost("http://localhost:8080/getHeader/HeaderServlet");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(httppost, responseHandler);
String result = response.toString();
}
但我不能,我也得到了错误:
But i'm unable to, and I get the error:
org.apache.http.conn.HttpHostConnectionException:Connection to http://localhost:8080 refused
我会很感激你的帮助。
I will be thankful your help
推荐答案
如果您从您的设备不是使用指的是本地主机
的 HTTP ://10.0.2.2/
而不是 http://127.0.0.1/
或的http://本地主机/
。
If you are referring to a localhost
from your device than use the http://10.0.2.2/
instead of the http://127.0.0.1/
or http://localhost/
.
由于你的Android模拟器上的虚拟机(QEMU)
运行您无法连接到服务器的PC上直接运行。
Because your Android emulator is running on a Virtual Machine(QEMU)
and you can not connect to a server directly running on your PC.
所以,你的code段将是这样的:
So your code snippet will be like this:
HttpPost httpMethod = new HttpPost("http://10.0.2.2:8080/getHeader/HeaderServlet");
转寄此:模拟器联网了解详情
这篇关于错误连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文