【问题标题】:SocketTimeout exception while connecting to a webservice from Android Device(Device only not emulator)从 Android 设备连接到 Web 服务时出现 SocketTimeout 异常(仅限设备而非模拟器)
【发布时间】:2012-06-24 01:22:09
【问题描述】:

我能够从我的 android 应用程序成功调用 .net 网络服务。用模拟器(2.2 apilevel 8 到 4.0.3 api level 15)对其进行了测试,效果很好。

我现在正尝试使用摩托罗拉 razr MAZZ 设备测试相同的代码。它已连接,能够打断点并且能够执行所有功能,除了调用 web 服务。我收到“java.net.SocketTimeoutException:连接超时”。 我的代码写在下面。它适用于模拟器,但不适用于设备。

URL = UtilsAct.Instance().GetSettingString(ctx, "sett_url");
String str="";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("IsEncrypted",false);
request.addProperty("routes", "EAST");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope); // java.net.SocketTimeoutException:    Connection timed out
Object receivedObj = envelope.getResponse();
str = receivedObj.toString();
}
catch(Exception e) 
{
//e.printStackTrace();
String errmsg = "Exception:";
if(e.getMessage() != null)
errmsg = errmsg +  e.getMessage();
}

我尝试了各种文章中给出的所有技巧,包括

HttpConnectionParams.setConnectionTimeout(params, timeout);

HttpConnectionParams.setSoTimeout(params, timeout);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(timeout);
urlConnection.setReadTimeout(timeout);

感谢帮助。 谢谢!

【问题讨论】:

  • “java.net.SocketTimeoutException: Connection timed out”表示服务器没有响应,或者没有及时响应。但是如果你不提供堆栈跟踪就很难说。
  • 谢谢克里斯汀。正如我所写,它在模拟器上工作得很好,所以服务器和代码和服务器权限都很好。
  • 正如我所说,如果您不发布堆栈跟踪,我能提供什么帮助?

标签: android web-services sockets timeout


【解决方案1】:

我真的很晚才更新。我发现托管 web 服务的服务器正在使用安全策略,并且不允许从任何使用 wifi 的设备进行呼叫。模拟器之所以有效,是因为它在网络内工作,因此不会干扰安全性。在我将服务器安全性更改为允许从使用我的 wifi 的设备进行呼叫后,我上面编写的代码现在可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-14
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多