如何在android中使用kso​​ap超时?

时间:2011-03-23 04:22:02

标签: android ksoap2 android-ksoap2

我正在使用kso​​ap2在Android中创建用户注册应用程序。

当我向服务器发送请求并且在10秒内没有响应时,我想用“再试一次”消息提示用户。如果服务器在10秒内响应,我希望程序在没有消息的情况下继续。

我怎样才能做到这一点?在KSoap2中是否存在TimerTask或任何超时方法?

1 个答案:

答案 0 :(得分:11)

private String METHOD_NAME;
    private String NAMESPACE;
    private String SOAP_ACTION;
    private String URL;
private int TimeOut=3000;//
    private SoapObject so;
    SoapSerializationEnvelope envelope;
    HttpTransportSE androidHttpTransport;
 try
               {      

                      METHOD_NAME = "myutility";
                      NAMESPACE = "http://";
                      SOAP_ACTION = NAMESPACE + METHOD_NAME;
                      Thread.sleep(2000);  
                      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                      envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                      envelope.setOutputSoapObject(request);

                      URL = "http://www.example.com";
                      androidHttpTransport = new HttpTransportSE(URL,Time_Out);
                      androidHttpTransport.call(SOAP_ACTION,envelope);
                      so = (SoapObject)envelope.bodyIn;
                      String s=so.toString();
                      //Your processing here
               }
               catch(InterruptedException e)
               {
                   //When timeout occurs handles this....

               }
catch( Exception e )
               {}