与无效 Web 服务的 Android 连接会使应用程序挂起



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

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

我该如何解决这个问题?如果未找到 Web 服务,如何阻止应用程序挂起?

我希望应用程序 UI 在找不到 Web 服务时保持原样。有什么方法可以避免应用程序挂起吗?

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

 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();
        }

}

ConnectionTimeOut 设置为HttpTransportSE对象。

使用最新的 using 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();
    }

最新更新