Android:调用Web服务时操作超时
我正在从 Android 应用程序调用 Web 服务。当我使用 AVD 时,一切正常,并且我有来自服务器的响应,但是当我在真实设备(HTC)上运行时,我收到“操作超时”。 设备上的互联网速度很好,例如谷歌地图加载速度非常快。
要与服务器连接,我使用 ksoap2:
SoapObject request = new SoapObject(getString(R.string.NAMESPACE), getString(R.string.METHOD_NAME));
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
int Timeout = 10000;
HttpTransportSE androidHttpTransport = new HttpTransportSE(FullURL, Timeout);
try {
androidHttpTransport.call(getString(R.string.SOAP_ACTION), envelope);
return envelope.getResponse().toString();
}
catch (Exception e) {
return ("ERROR:" + e.getMessage());
}
URL 包含服务器的 IP,例如
为什么实际会出现超时错误设备以及如何强制其正常工作?
I'm calling a web service from Android application. When I use AVD it's all right and I have response from server, but when I run on my real device (HTC), I get "The operation time out".
The Internet speed on device is good, for example Google Maps are loading very fast.
To connect with server I use ksoap2:
SoapObject request = new SoapObject(getString(R.string.NAMESPACE), getString(R.string.METHOD_NAME));
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
int Timeout = 10000;
HttpTransportSE androidHttpTransport = new HttpTransportSE(FullURL, Timeout);
try {
androidHttpTransport.call(getString(R.string.SOAP_ACTION), envelope);
return envelope.getResponse().toString();
}
catch (Exception e) {
return ("ERROR:" + e.getMessage());
}
URL contains the server's IP like
Why there's a time out error on real device and how to force it to work normally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须为现实生活中的设备设置连接超时和套接字超时。您必须适当地设置超时值。超时后,将收到异常,捕获该异常并显示“网络不良”等警报。
You have to set the connection time out and socket timeout for real life devices. You has to set the timeout values appropriately. After that time out an exception will be recieved, catch that exception and show an alert like "Bad network".
我遇到了同样的问题。我找到的解决方案是手机使用 WiFi 或 3g 连接,并且我提到的网络服务有某种防火墙。
因此,我将 URL 中的 IP 地址(例如:“http://10.0.0.0”)替换为主机名(例如:“http://abc.com”)。
I was getting the same problem. The solution I found was that the phone uses WiFi or 3g connections, and the web service I was referring had some kind of firewall.
So, I replaced the IP address in the URL (example: 'http://10.0.0.0') with a hostname (example: 'http://abc.com').
我在开发项目时遇到这种情况,并找到了下面描述的解决方案:
请从控制面板 -> > 关闭您的 Windows 防火墙。 Windows 防火墙,然后单击“打开或关闭 Windows 防火墙”以关闭 Windows 防火墙。
然后您的应用程序就可以正常工作了。
I face this situation while developing my project and found the solution which I describe below :
Please turn off your Windows Firewall from
Control Panel -> Windows Firewall and click on Turns Windows Firewall on or off to turn off your Windows Firewall.
Then your application works fine.
我通过在开发计算机上的防火墙中创建一条规则以允许端口 80 上的远程连接解决了类似的问题。
I solved the similar issue by creating a rule in Firewall on my development machine to allow remote connections on port 80.
当我在本地服务器上测试我的应用程序时,我遇到了这种类型的错误。我使用了系统的 IP 地址,它工作正常。如果有人遇到同样的问题,请先检查您的 url 的 IP 地址。希望这对正在测试的其他人有帮助本地服务器上有应用程序。
昨天我使用这个链接进行名为 http://192.168.1.7 的 Web 服务/DemoAndroid/Loginwebservice.php?username=john&dob=1234。
它工作正常,但是今天当我使用相同的链接时它给了我错误
所以我检查了我的系统 IP 地址,它与 url 中提到的不一样。所以我复制了该 IP 地址并粘贴到上面的 url 中,它再次工作。
I got this type of error when I was testing my application on local server.I used IP address of the system,it works fine.If anyone got same issue,please check IP address of your url first.Hope this help others who are testing there application on local server.
Yesterday I was using this link for web service called http://192.168.1.7/DemoAndroid/Loginwebservice.php?username=john&dob=1234.
and it work fine,but today when I used same link it give me error
so I checked my system IP address and It was not same as mentioned in url.so I copied that IP address and Paste in above url and it works again.