Android okHttp重试策略(Android okHttp Retry policy)
我正在尝试创建一个简单的包装器,它将调用服务器下载信息并解析发送的二进制数据。 对于连接我正在使用名为okhttp的库,因为3G上的连接不是很可靠我决定使用以下函数实现一个非常简单的重试功能**(注意这个方法总是从后台线程调用)**
private InputStream callServer() throws ServerException, NoNetworkAvailableException, ConnectionErrorException { NetworkOperation networkOperation = getNetworkOperation(); InputStream inputStream = null; //in case of network problems we will retry 3 times separated by 5 seconds before gave up while (connectionFailedRetryCounter < connectionFailedMaximumAllowedRetries()) { connectionFailedRetryCounter++; try { inputStream = networkOperation.execute(); break;//if this line was reached it means a successfull operation, no need to retry . } catch (ConnectionErrorException e) { if (canRetryToConnect()) { Utils.forceSleepThread(Constants.Communications.ConnectionFailedTrialCounter.SLEEP_BETWEEN_REQUESTS_MILLI);//retry after 5 secs (Thread.sleep) } else { throw e;//I give up } } } return inputStream; } private boolean canRetryToConnect() { return (connectionFailedRetryCounter < connectionFailedMaximumAllowedRetries()) && !canceled; }
这是正确的方法吗? 或者它已经由库自己完成(没有必要实现这样的东西)?
这是execute()方法的作用
public InputStream execute() throws ConnectionErrorException, NoNetworkAvailableException, ServerException { if (!Utils.isNetworkAvailable(context)) { throw new NoNetworkAvailableException(); } Response response = doExecute(); if (!response.isSuccessful()) { throw new ServerException(response.code()); } return response.body().byteStream(); } private Response doExecute() throws ConnectionErrorException { Response response; try { if (getRequestType() == RequestType.GET) { response = executeGet(); } else { response = executePost(); } } catch (IOException e) { throw new ConnectionErrorException(); } return response; }
I am trying to create a simple wrapper which will call the server download the information and parse the binary data sent . for the connection I am using the library called okhttp , since the connection on 3G is not very reliable I have decided to implement a very simple re-try functionality using the following function**(Note this method will be always called from a background thread)**
private InputStream callServer() throws ServerException, NoNetworkAvailableException, ConnectionErrorException { NetworkOperation networkOperation = getNetworkOperation(); InputStream inputStream = null; //in case of network problems we will retry 3 times separated by 5 seconds before gave up while (connectionFailedRetryCounter < connectionFailedMaximumAllowedRetries()) { connectionFailedRetryCounter++; try { inputStream = networkOperation.execute(); break;//if this line was reached it means a successfull operation, no need to retry . } catch (ConnectionErrorException e) { if (canRetryToConnect()) { Utils.forceSleepThread(Constants.Communications.ConnectionFailedTrialCounter.SLEEP_BETWEEN_REQUESTS_MILLI);//retry after 5 secs (Thread.sleep) } else { throw e;//I give up } } } return inputStream; } private boolean canRetryToConnect() { return (connectionFailedRetryCounter < connectionFailedMaximumAllowedRetries()) && !canceled; }
Is this the right way to do this ? or is it already done by the library it self(there is no need to implement anything like this) ?
Here is what the method execute() do
public InputStream execute() throws ConnectionErrorException, NoNetworkAvailableException, ServerException { if (!Utils.isNetworkAvailable(context)) { throw new NoNetworkAvailableException(); } Response response = doExecute(); if (!response.isSuccessful()) { throw new ServerException(response.code()); } return response.body().byteStream(); } private Response doExecute() throws ConnectionErrorException { Response response; try { if (getRequestType() == RequestType.GET) { response = executeGet(); } else { response = executePost(); } } catch (IOException e) { throw new ConnectionErrorException(); } return response; }
原文:https://stackoverflow.com/questions/35057112
最满意答案
你可以这样做:
var cards = document.getElementsByClassName('card'); for (var i = 0; i < cards.length; i++) { cards[i].onclick = function(target) { // Turn the 'cards' into an array and loop over it. [].forEach.call(cards, function(card) { card.innerHTML = "down" }); this.innerHTML = "up"; } }
这将遍历所有卡并将其值返回
"down"
。演示如下
var cards = document.getElementsByClassName('card'); for (var i = 0; i < cards.length; i++) { cards[i].onclick = function(target) { [].forEach.call(cards, function(card) { card.innerHTML = "down" }); this.innerHTML = "up"; } }
<table> <tr> <td class="card">down</td> <td class="card">down</td> </tr> <tr> <td class="card">down</td> <td class="card">down</td> </tr> </table>
You can do it like:
var cards = document.getElementsByClassName('card'); for (var i = 0; i < cards.length; i++) { cards[i].onclick = function(target) { // Turn the 'cards' into an array and loop over it. [].forEach.call(cards, function(card) { card.innerHTML = "down" }); this.innerHTML = "up"; } }
This will loop over all the cards and return their values to
"down"
.Demo below
var cards = document.getElementsByClassName('card'); for (var i = 0; i < cards.length; i++) { cards[i].onclick = function(target) { [].forEach.call(cards, function(card) { card.innerHTML = "down" }); this.innerHTML = "up"; } }
<table> <tr> <td class="card">down</td> <td class="card">down</td> </tr> <tr> <td class="card">down</td> <td class="card">down</td> </tr> </table>
相关问答
更多-
这是因为你的标记是错误的。标记不是自动关闭的,所以你不能用/>关闭它。 您必须使用结束标记,如下所示:
innerHTML类(innerHTML class)[2022-06-28]
JavaScript的: element.className = 'foo'; // or element.className += ' foo'; CSS: .foo { color: #F00; font-size: 2em; } 或者,更高级: elem.classList.add( 'foo' ); (像往常一样,IE通过条件评论获取polyfill 。) - ŠimeVidas JavaScript: element.className = 'foo'; // or eleme ...JavaScript通过.value或.innerHTML获取TextArea输入?(JavaScript get TextArea input via .value or .innerHTML?)[2023-11-06]
你应该使用.value myTextArea.value You should use .value myTextArea.value你可以这样做: var cards = document.getElementsByClassName('card'); for (var i = 0; i < cards.length; i++) { cards[i].onclick = function(target) { // Turn the 'cards' into an array and loop over it. [].forEach.call(cards, function(card) { card.in ...JS没有块范围,因此所有identificator绑定实际上是最后更新的值。 用函数调用包装它来创建一个独立的闭包: for (var i=0; i < found_elements.length; i++) { (function(i) { // do your job here with i })(i) } 或者使用forEach方法,每次迭代都有自己的范围: found_elements.forEach(function(element, i) { // do your job ...ID需要是唯一的 - 你应该尝试用id="filename"替换class="filename"所有元素,然后用你的each()函数中的.filename替换#filename ,所以它类似于以下内容: $(".filename").each(function() { $(this).html("ext!"); }); ID's need to be unique - you should try replacing all elements with id="filename" to clas ...更改.innerHTML属性(Change .innerHTML property)[2022-05-08]
document.getElementById(“verifyButton”)。setAttribute(“data-dismiss”,“Modal-or-anything”); document.getElementById("verifyButton").setAttribute("data-dismiss", "Modal-or-anything");尝试这个 var songtitle = document.getElementById('songtitle'); var dropdownList = document.getElementsByClassName('dropdownList'); function run(event) { songtitle.innerHTML =event.target.innerHTML; //console.log(event.target.innerHTML); } //console.log( ...点击元素的innerHTML(innerHTML of clicked element)[2022-10-15]
您可以将一个事件附加到文档onclick处理程序。 无需遍历页面上的每个元素。 点击将冒泡到它... document.onclick = function(event) { var target = event.target || event.srcElement; alert ( target.innerHTML ); }; 不过,我建议使用更好的方法来附加事件处理程序,而不是'=',因为它会覆盖以前定义的任何可能对页面操作至关重要的事件。 我之前使用过此片段,以便在没有jQuer ...相关文章
更多- Retry or Not Retry
- android布局问题
- 《Android内核剖析》扫描版[PDF]
- Professional Android 4 Application Development
- 疯狂Android讲义
- Android学习之 UI效果
- 《android 开发书籍》
- android 圆角item shape
- 请问下,ophone sdk对应的android版本是几呢?Android开发的应用能否在ophone上运行嗯?
- Android分类前言
最新问答
更多- h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
- 创建一个功能(Create a function)
- 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
- PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
- Qt冻结循环的原因?(Qt freezing cause of the loop?)
- TableView重复youtube-api结果(TableView Repeating youtube-api result)
- 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
- SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
- 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
- 以编程方式创建UITableView(Creating a UITableView Programmatically)
- 如何打破按钮上的生命周期循环(How to break do-while loop on button)
- C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
- 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
- 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
- Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
- 设置kotlin扩展名(Setting a kotlin extension)
- 如何并排放置两个元件?(How to position two elements side by side?)
- 如何在vim中启用python3?(How to enable python3 in vim?)
- 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
- 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
- Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
- dedecms如何安装?
- 在哪儿学计算机最好?
- 学php哪个的书 最好,本人菜鸟
- 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
- 如何覆盖错误堆栈getter(How to override Error stack getter)
- 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
- USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
- 电脑高中毕业学习去哪里培训
- 正则表达式验证SMTP响应(Regex to validate SMTP Responses)