在异步任务中尝试/捕获

时间:2011-09-01 16:14:38

标签: android

我实现了一个Async任务,该任务应该从webservice获取数据 - 但是如果设备没有活动的InternetConnection - 或者如果在“try”块中发生任何事情我得到一个异常并且我的应用程序崩溃...我试图阻止带有“finally”子句的行为..但是调试器没有输入finally子句

            try{
                this.kantinen = new KantinenListe();
                Gson gson = new Gson();             
                // SOAP Test
                String NAMESPACE = "http://tempuri.org/";
                String METHOD_NAME = "fullSyncGPS";
                String SOAP_ACTION = "http://tempuri.org/IDatenService/fullSyncGPS";
                String URL = "Currywurst.svc?wsdl";

                SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

                PropertyInfo pi = new PropertyInfo();
                request.addProperty("radius",10);
                request.addProperty("lat", Double.toString(currentLocation.getLatitude()));
                request.addProperty("lng", Double.toString(currentLocation.getLongitude()));
                request.addProperty("von", "01.09.2011");
                request.addProperty("bis", "05.09.2011");

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);        

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

                String resultData = result.toString(); 


                resultData = "{\"meineKantinen\":"+resultData+"}";
                this.kantinen = gson.fromJson(resultData, KantinenListe.class);
            }
            catch(Exception e)
            {
          //todo: implement
            }
            finally
            {
                return this.kantinen;
            }

3 个答案:

答案 0 :(得分:0)

catch(Exception e)
{
     return this.kantinen;
{

可能是一个可能的解决方案吗?但是我不知道为什么你最终没有执行

答案 1 :(得分:0)

如果从此方法返回时未初始化this.kantinen,则会发生崩溃。确保你至少有类似的东西:

 KantinenListe kantinen = null;

答案 2 :(得分:0)

**您可以检查网络连接,然后进行处理**

public static boolean isNetworkAvailable(Context context){         boolean outcome = false;

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();

    for (NetworkInfo tempNetworkInfo : networkInfos) {

        if (tempNetworkInfo.isConnected()) {
            outcome = true;
            break;
        }
    }


    return outcome;
}