How do I enforce a timeout on a webservice call using ksoap 2?

喜欢而已 提交于 2019-12-19 10:08:17

问题


I need to add a timeout to a J2ME application that uses ksoap 2 to connect to a web service.

I've tried the method described as a possible pseudo timeout at http://ksoap2.sourceforge.net/doc/api/org/ksoap2/transport/HttpTransport.html, but it doesn't seem to function on this device.

I'd run the connection on another thread and kill it if a timer fires but there's no way to kill a thread before it finishes executing in J2ME per http://developers.sun.com/mobility/midp/articles/threading2/ (this is an embedded device, so I can't just leave an indefinite number of threads blocking in the background). I can't use the poll a boolean method since it's the single attempt to open the connection that blocks.

The system timeout seems to vary between device modal and is too long for my purposes.

Does anybody have any thoughts as to something that might work?


回答1:


I ended up using the Socket class which has the setSoTimeout() method.




回答2:


Could mention that I made a modification to the KSoap2 v2.5.2 to support timeout for HttpTransportSE class. It will throw a SocketTimeoutException when timeout occurs.

It's both jar and src is found at this url http://www.lightsoft.se/?p=707




回答3:


Keep in mind you are not dealing with fully functional computers. On some devices, you just can't interrupt network operations, especially the TCP connect.

This is what we do,

  1. Before making the connection, create another monitoring timer thread on a a short frequency (say 2 seconds).
  2. In the monitoring thread, you can send some message to the device pretending you are making progress if time limit is not reached.
  3. If a certain time limit is reached, try to interrupt the other thread by sending Thread.interrupt(). This call is available in MIDP.
  4. On the connection thread, just quit if being interrupted.

This works great on all emulators but the connection thread doesn't get the exception till 5 minutes later on some phones.



来源:https://stackoverflow.com/questions/1473810/how-do-i-enforce-a-timeout-on-a-webservice-call-using-ksoap-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!