最后使用回调监听器

时间:2017-05-24 14:02:48

标签: java android try-catch

所以,我有以下代码:

try{
....
    if(serverResp.isSucces()){
      callbackListener.onDataLoaded(serverResp);
    }

 }catch(Exception e){
 //...do whatever I have to
}finally{
  urlConnection.disconnect();
}

我的问题是,当调用urlConnection.disconnect时?大多数示例最后解释了在返回的情况下调用它的时间。我理解这种情况,但在这里我没有回复,而是打电话给听众。 在大多数情况下,侦听器回调会触发一个新的Activity来启动。我想确定,我之前的所有连接都已关闭!

所以主要的问题是:

  • 当最后一次调用时,如果没有返回但是监听器回调?
  • 这是关闭urlConnection的正确方法吗?

1 个答案:

答案 0 :(得分:0)

finally是关闭资源的最佳位置。

finally块在退出try / catch块之后执行。这种情况发生在:

  1. 到达try块的结尾
  2. try块以任何其他方式退出(外部循环breakreturn,抛出未捕获的异常等。)
  3. 捕获异常并执行到达catch
  4. 的末尾
  5. 执行此代码的线程终止。
  6. finally只能disconnect执行var app = angular.module('TestApp', []); app.directive('selectMultiple', function() { return { restrict: 'A', link: function(scope, element, attrs) { $(element).find('select').material_select(); $(element).find("ul li").first().click(function() { $(element).find("ul li:not(:first-child)").each(function(index) { $(this).find("input[type=checkbox]").prop("checked", $(element).find("ul li").first().find("input[type=checkbox]").prop("checked")); }); }); } }; }); 的唯一时间。

    在您的情况下,假设没有抛出异常,您的回调将在连接关闭之前执行。回调完成后将关闭连接(通过完全执行或委托给其他某个线程)。

    请注意,Failed to create provisioning profile. The app ID "<bundle-id>" cannot be registered to your development team. Change your bundle identifier to a unique string to try again. 可能只是确保在较新版本的Java中关闭连接的安全方法。您通常会尝试读取整个响应,然后关闭输入流。您可以在本网站上找到关于此的大量讨论。