Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 截击-在超时标记处重复POST呼叫_Java_Android_Android Volley - Fatal编程技术网

Java 截击-在超时标记处重复POST呼叫

Java 截击-在超时标记处重复POST呼叫,java,android,android-volley,Java,Android,Android Volley,我遇到了一个问题,当截击击中超时标记时,会发出重复的POST请求。我通过DefaultRetryPolicy设置超时,同时将重试次数设置为0。我尝试了其他一些组合,但没有改变行为 volleyRequest.setRetryPolicy(new DefaultRetryPolicy(timeout*1000, 0, 1f)); 我还尝试扩展DefaultRetryPolicy类,以确保 retry(VolleyError error) 在任何时候都不会尝试重试的情况下抛出错误 class c

我遇到了一个问题,当截击击中超时标记时,会发出重复的POST请求。我通过DefaultRetryPolicy设置超时,同时将重试次数设置为0。我尝试了其他一些组合,但没有改变行为

volleyRequest.setRetryPolicy(new DefaultRetryPolicy(timeout*1000, 0, 1f));
我还尝试扩展DefaultRetryPolicy类,以确保

retry(VolleyError error)
在任何时候都不会尝试重试的情况下抛出错误

class customRetryPolicy extends DefaultRetryPolicy{
    private int mCurrentTimeoutMs;

    /** The current retry count. */
    private int mCurrentRetryCount;

    /** The maximum number of attempts. */
    private final int mMaxNumRetries;

    /** The backoff multiplier for the policy. */
    private final float mBackoffMultiplier;

    /** The default socket timeout in milliseconds */
    public static final int DEFAULT_TIMEOUT_MS = 10000;

    /** The default number of retries */
    public static final int DEFAULT_MAX_RETRIES = 0;

    /** The default backoff multiplier */
    public static final float DEFAULT_BACKOFF_MULT = 1f;

    /**
     * Constructs a new retry policy using the default timeouts.
     */
    public customRetryPolicy() {
        this(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT);
    }

    /**
     * Constructs a new retry policy.
     * @param initialTimeoutMs The initial timeout for the policy.
     * @param maxNumRetries The maximum number of retries.
     * @param backoffMultiplier Backoff multiplier for the policy.
     */
    public customRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoffMultiplier) {
        mCurrentTimeoutMs = initialTimeoutMs;
        mMaxNumRetries = maxNumRetries;
        mBackoffMultiplier = backoffMultiplier;
    }

    /**
     * Returns the current timeout.
     */
    @Override
    public int getCurrentTimeout() {
        return mCurrentTimeoutMs;
    }

    /**
     * Returns the current retry count.
     */
    @Override
    public int getCurrentRetryCount() {
        return mCurrentRetryCount;
    }

    /**
     * Returns the backoff multiplier for the policy.
     */
    public float getBackoffMultiplier() {
        return mBackoffMultiplier;
    }

    /**
     * Prepares for the next retry by applying a backoff to the timeout.
     * @param error The error code of the last attempt.
     */
    @Override
    public void retry(VolleyError error) throws VolleyError {
        throw error;
    }

    /**
     * Returns true if this policy has attempts remaining, false otherwise.
     */
    protected boolean hasAttemptRemaining() {
        return false;
    }
}
但是,在添加日志之后,当超时标记命中时,我看不到该方法被触发。它确实会为其他错误触发,然后会适当地抛出错误


还有其他人经历过这个问题吗

这可能是因为Volley遇到网络错误时会自动重试。不过,我对它在其他网络错误上重试没有问题。似乎只有在通话时间过长时才会这样做。同样值得注意的是,如果我将超时时间缩短到5秒左右,我就没有这个问题。每件事都按它应该的方式被调用。当我延长超时长度时,问题就出现了。这可能是一个bug。那么为什么不使用5秒的超时时间呢?不幸的是,我无法设置超时规则。我希望找到解决此问题的解决方案,但到目前为止我还无法找到。我在这里找到了解决方案:[Android Volley在重试策略设置为0时向服务器发出2个请求][1](请参阅回答中的“编辑”段落)[1]: