0

我的应用程序使用 ksoap2 连接到 Web 服务。该应用程序使用户能够更改提供 Web 服务的服务器的 IP 地址/URL(在设置中)。

问题是当用户将IP地址/URL更改为无效位置时,应用程序长时间挂起,LogCat显示应用程序无法连接到提供的Socket描述。

我该如何解决 ?如果找不到 Web 服务,如何停止应用程序挂起?

如果找不到 Web 服务,我希望应用程序 UI 保持原样。有什么办法可以避免应用程序挂起?

这是我的代码。请提出任何避免应用程序挂起的方法。

 private void getControlFromServer()  {
    // TODO Auto-generated method stub

    SoapObject request = new SoapObject(NAMESPACE, METHOD_GET_CONTROL);
    SoapSerializationEnvelope envelope = 
        new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);


HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);




     try {
             androidHttpTransport.call(SOAP_ACTION_GET_CONTROL, envelope);




            SoapObject result=(SoapObject)envelope.getResponse(); //To get the data.

            tvTemp.setText("Received :" + result.getProperty(0).toString()+ " : " + result.getProperty(1).toString() +  " : " + result.getProperty(2).toString() +  " : " + result.getProperty(3).toString() +  " : " + result.getProperty(4).toString() +  " : " + result.getProperty(5).toString() +  " : " + result.getProperty(6).toString());

     } catch (Exception e) {


            tvTemp.setText("Error");
            e.printStackTrace();

        }


}
4

1 回答 1

1

ConnectionTimeOut设置为您的HttpTransportSE对象..

使用最新 使用ksoap2 API 版本 2.5.7或更高版本,

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport;
SoapObject response = null;

try {
        androidHttpTransport = new HttpTransportSE(URL,6000); // Tried Setting the Timeout to 1 Minute
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION,envelope);
        response = (SoapObject)envelope.bodyIn;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
于 2012-06-20T11:18:15.740 回答