简体   繁体   English

从android httpclient程序访问Servlet导致连接被拒绝?

[英]Accessing Servlet from android httpclient programs getting connection refused?

While I access the localhost servlet from android httpclient utils I am getting connection refused error. 当我从android httpclient utils访问localhost servlet时,出现connection refused错误。 The servlet is running fine, but I am getting error. Servlet运行正常,但出现错误。

This is the code: 这是代码:

String dmurl="http://127.0.0.1:9090/DataManagerProject/DMServlet";                                  
HttpClient httpClient = new DefaultHttpClient();
HttpGet hrtreq = new HttpGet(dmurl);
HttpResponse resp = httpClient.execute(hrtreq);
String output = EntityUtils.toString(resp.getEntity());

I am getting following error org.apache.http.conn.httphostconnectexception connection to refused 我收到以下错误org.apache.http.conn.httphostconnectexception connection to refused

if you are using emulator to run your app for local server. 如果您使用模拟器为本地服务器运行应用程序。 mention the local ip as 10.0.2.2 and have to give Internet permission into your app 提及本地IP为10.0.2.2并且必须向您的应用授予Internet权限

For more details, please refer this link . 有关更多详细信息,请参阅此链接

Also, Turn Off the firewall and any anti-virus application in your PC 另外, 关闭PC中的防火墙和所有防病毒应用程序

Check whether you have internet access enabled in your emulator and in your application 检查您的模拟器和应用程序中是否启用了Internet访问

<uses-permission android:name="android.permission.INTERNET"/> 

To access internet data of the device or emulator from your application add the above line in your manifest xml file.Also check the servlet mapping is proper in your the servlet project. 要从您的应用程序访问设备或仿真器的Internet数据,请在清单xml文件中添加以上行。还要检查servlet项目中的servlet映射是否正确。

Assuming your servlet runs on your computer the reason you can't get a connection is because your using the wrong IP. 假设您的servlet在计算机上运行,​​则无法建立连接的原因是因为使用了错误的IP。 127.0.0.1 is a loopback address meaning that if you connect to 127.0.0.1 on your Android device, it will try to connect to the Android device. 127.0.0.1是回送地址,这意味着如果您在Android设备上连接到127.0.0.1,它将尝试连接到Android设备。

To make it work you have to find the IP address of the computer which runs the servlet and use that in the URL instead. 要使其正常工作,您必须找到运行servlet的计算机的IP地址,并在URL中使用它。 In addition make sure you have given the Internet permission to your app. 此外,请确保您已为您的应用授予Internet权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM