Java 如何将android设备连接到本地主机服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24840366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:06:44  来源:igfitidea点击:

How to connect android device to localhost server?

javaandroid

提问by user001

I am trying to establish a connection between my client app running on my android mobile and a server running on my windows 7 PC.

我正在尝试在我的 android 手机上运行的客户端应用程序和我的 Windows 7 PC 上运行的服务器之间建立连接。

I am new to android and so while looking for some client server tutorial, got the below 2 links.

我是 android 新手,所以在寻找一些客户端服务器教程时,得到了以下 2 个链接。

http://www.compiletimeerror.com/2013/09/creating-java-web-service-using-axis-2.html

http://www.compiletimeerror.com/2013/09/creating-java-web-service-using-axis-2.html

http://www.compiletimeerror.com/2013/09/accessing-web-service-from-android.html

http://www.compiletimeerror.com/2013/09/accessing-web-service-from-android.html

Here, server is developed on java and published using axis2 webservice. I followed the tutorial and could develop the similar client and server which runs fine on the emulator. But if I try to access the same using my android mobile it is not working. My mobile is unable to speak to the localhost on my PC.

在这里,服务器是在 java 上开发的,并使用axis2 webservice 发布。我遵循了教程,可以开发在模拟器上运行良好的类似客户端和服务器。但是,如果我尝试使用我的 android 手机访问相同的内容,则无法正常工作。我的手机无法与 PC 上的本地主机通话。

What I am doing here is,

我在这里做的是,

1. Connected both my PC and mobile to same WIFI network.
2. Connected the mobile to PC using USB.
3. Started the Server on Tomcat
4. Running the Client Project from eclipse and selecting my mobile as target.

Below is my published webservice URL which I am calling from client code,

下面是我从客户端代码调用的已发布的 Web 服务 URL,

http://192.168.0.3:8080/MyWebService/services/WebService?wsdl

Here 192.168.0.3is my PC IP

这里 192.168.0.3是我的电脑 IP

Please let me know what am I doing wrong here?

请让我知道我在这里做错了什么?

采纳答案by ThomasEdwin

Windows 7 might block port 8080. You could test that your port is open from another pc using

Windows 7 可能会阻止端口 8080。您可以使用以下命令测试您的端口是否已从另一台电脑打开

telnet 192.168.0.3 8080

or from android using this code.

从 android 使用此代码

If 8080 is blocked, then you may want to open it using these steps:

如果 8080 被阻止,那么您可能需要使用以下步骤打开它:

  1. Open Windows Firewall by clicking the Start button Picture of the Start button, and then clicking Control Panel. In the search box, type firewall, and then click Windows Firewall.
  2. In the left pane, click Advanced settings. Administrator permission required If you're prompted for an administrator password or confirmation, type the password or provide confirmation.
  3. In the Windows Firewall with Advanced Security dialog box, in the left pane, click Inbound Rules, and then, in the right pane, click New Rule.
  4. Follow the instructions in the New Inbound Rule wizard.
  1. 单击“开始”按钮“开始”按钮的图片,然后单击“控制面板”,打开 Windows 防火墙。在搜索框中,键入 firewall,然后单击 Windows 防火墙。
  2. 在左窗格中,单击高级设置。需要管理员权限 如果系统提示您输入管理员密码或确认,请键入密码或进行确认。
  3. 在“高级安全 Windows 防火墙”对话框的左窗格中,单击“入站规则”,然后在右窗格中单击“新建规则”。
  4. 按照新建入站规则向导中的说明进行操作。

Note: You must be sure beforehand that your android device is connected to the same local area network or wifi.

注意:您必须事先确保您的 android 设备连接到相同的局域网或 wifi。

回答by JIthin

Try to access your URL

尝试访问您的网址

http://192.168.0.3:8080/MyWebService/services/WebService?wsdl

from your android browser. If you are getting an xml page, then it's not the network problem. If it's network problem, try restarting your local server and changing firewall and router configurations.

从你的安卓浏览器。如果你得到一个 xml 页面,那么这不是网络问题。如果是网络问题,请尝试重新启动本地服务器并更改防火墙和路由器配置。

You have to provide more information to get a specific answer.

您必须提供更多信息才能获得具体答案。

回答by Mehul

If everything is fine then u should check your Android App. U have to add the following permission for Internet access.

如果一切正常,那么您应该检查您的 Android 应用程序。您必须添加以下权限才能访问 Internet。

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