本文介绍了错误连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要建立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("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 localhost:8080 refused
我会很感激你的帮助。
I will be thankful your help
推荐答案如果您从您的设备不是使用指的是本地主机的 HTTP ://10.0.2.2/ 而不是 127.0.0.1/ 或的http://本地主机/ 。
If you are referring to a localhost from your device than use the 10.0.2.2/ instead of the 127.0.0.1/ or 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("10.0.2.2:8080/getHeader/HeaderServlet");
转寄此:模拟器联网了解详情
更多推荐
错误连接被拒绝
发布评论