错误连接被拒绝被拒、错误

由网友(如梦亦如幻)分享简介:我要建立HTTP连接到我自己的servlet。这是我的code:I want to make an Http Connection to my own servlet. Here is my code:try{HttpClient client = new DefaultHttpClient();HttpPost...

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

由于你的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("http://10.0.2.2:8080/getHeader/HeaderServlet");

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

阅读全文

相关推荐

最新文章