如何使用OkHttp禁用一个连接的重试?

时间:2015-10-15 18:54:07

标签: android okhttp

我正在使用OkHttp并希望在特定的api调用上禁用连接重试。这是正确的方法吗?:

mMyGlobalClient = new OkHttpClient();

....

public void makeConnection(...) {

    OkHttpClient client = null;
    if (disableRetries) {
        OkHttpClient clone = mMyGlobalClient.clone();
        clone.setRetryOnConnectionFailure(false);
        client = clone;
    } else {
        client = mMyGlobalClient;
    }

    client.newCall(...);
}

这个想法来自这篇文章:

https://github.com/square/okhttp/pull/1259#issue-53157152

  

大多数应用程序不希望全局禁用重试。相反,使用   clone()获取特定的非幂等请求的OkHttpClient,   然后使用设置配置该客户端。

我想要禁用此次调用的重试的原因是因为如果我的服务器处理了两次,它可能具有破坏性:

https://github.com/square/okhttp/pull/1259#issuecomment-68430264

谢谢

3 个答案:

答案 0 :(得分:4)

是的,这是正确的方法。

顺便说一下,如果你不介意,你可以写一点简单

mMyGlobalClient = new OkHttpClient();

....

public void makeConnection(...) {

    OkHttpClient client = null;
    if (disableRetries) {
        client = mMyGlobalClient.clone();
        client.setRetryOnConnectionFailure(false);
    } else {
        client = mMyGlobalClient;
    }

    client.newCall(...);
}

答案 1 :(得分:0)

使用<!DOCTYPE html> <meta charset="utf-8"> <style> body, html { margin: 0;width: 100%; height: 100%} circle { } </style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"></script> <script> var width = $('body').width(), height = $('body').height(), padding = 10, // separation between nodes maxRadius = 40; var n = 10, // total number of nodes m = 1; // number of distinct clusters var color = d3.scale.category10() .domain(d3.range(m)); var x = d3.scale.ordinal() .domain(d3.range(m)) .rangePoints([width - 200, width], 1); var nodes = d3.range(n).map(function() { var i = Math.floor(Math.random() * m), v = (i + 1) / m * -Math.log(Math.random()); return { radius: Math.random() * maxRadius + 30, color: color(i), cx: x(i), cy: height }; }); var force = d3.layout.force() .nodes(nodes) .size([width, height]) .gravity(0) .charge(0) .on("tick", tick) .start(); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); var circle = svg.selectAll("circle") .data(nodes) .enter().append("circle") .attr("r", function(d) { return d.radius; }) .style("fill", function(d) { return d.color; }) .call(force.drag); function tick(e) { circle .each(gravity(.2 * e.alpha)) .each(collide(.5)) //.attr("cx", function(d) { return d.x; }) //.attr("cy", function(d) { return d.y; }); .attr("cx", function(d) { return d.x = Math.max(d.radius, Math.min(width - d.radius, d.x)); }) .attr("cy", function(d) { return d.y = Math.max(d.radius, Math.min(height - d.radius, d.y)); }); } // Move nodes toward cluster focus. function gravity(alpha) { return function(d) { d.y += (d.cy - d.y) * alpha; d.x += (d.cx - d.x) * alpha; }; } // Resolve collisions between nodes. function collide(alpha) { var quadtree = d3.geom.quadtree(nodes); return function(d) { var r = d.radius + maxRadius + padding, nx1 = d.x - r, nx2 = d.x + r, ny1 = d.y - r, ny2 = d.y + r; quadtree.visit(function(quad, x1, y1, x2, y2) { if (quad.point && (quad.point !== d)) { var x = d.x - quad.point.x, y = d.y - quad.point.y, l = Math.sqrt(x * x + y * y), r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding; if (l < r) { l = (l - r) / l * alpha; d.x -= x *= l; d.y -= y *= l; quad.point.x += x; quad.point.y += y; } } return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1; }); }; } </script>

call.cancel();

如果要限制重试次数,可以使用:

final Call call = client.newCall(request);

答案 2 :(得分:0)

此外,我已经在Retrofit2中解决了这个问题。

为防止重试,您必须对OkHttpClient使用.retryOnConnectionFailure(false)方法。

示例代码:

OkHttpClient okHttpClient= null;
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(2, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
                .retryOnConnectionFailure(false)
                .cache(null)//new Cache(sContext.getCacheDir(),10*1024*1024)
                .build();

在以前的版本中,经过改版后存在一些错误并已修复:2.1.0

因此,您应该使用Retrofit2和okhttp3的更新版本:

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.4.1' 

问题讨论:

https://github.com/square/okhttp/pull/1259#issuecomment-68430264