错误连接被拒绝

编程入门 行业动态 更新时间:2024-10-27 18:19:14
本文介绍了错误连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要建立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/.

由于你的Andr​​oid模拟器上的虚拟机(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");

转寄此:模拟器联网了解详情

更多推荐

错误连接被拒绝

本文发布于:2023-10-16 10:10:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被拒   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!