Okhttp无限循环改变连接状态

时间:2015-05-12 11:39:29

标签: android httpurlconnection okhttp

我有使用okHttp lib发出请求的简单代码:

    Request request = new Request.Builder()
            .url(url)
            .build();
    Log.d("TAG", "start");
    Response response = httpClient.newCall(request).execute();
    Log.d("TAG", "stop");

一切正常,除非我尝试更改连接,同时执行请求。例如,如果我尝试连接没有wifi(具有慢速互联网连接),并在正在进行中打开wifi,它将无限执行。但如果我留在慢速移动互联网或保持wifi(不改变连接类型)一切正常。当我关闭所有连接,开始执行然后打开连接时,同样的故事。 我试图使用超时,但它没有帮助。

编辑。没有其他有价值的代码:

private class DownloadXmlTask extends AsyncTask<String, Void, Void> {
    private AsyncCallback<Reader> callback;

    private DownloadXmlTask(AsyncCallback<Reader> callback) {
        this.callback = callback;
    }

    @Override
    protected Void doInBackground(String... urls) {
        try {
            Reader result = doGetRequest(urls[0]);
            callback.success(result);
        } catch (IOException e) {
            log.debug(context.getResources().getString(R.string.error_http_request), e);
            callback.failure(context.getResources().getString(R.string.error_http_request), e);
        }
        return null;
    }

}

private Reader doGetRequest(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Log.d("MYTAG", "start");
        Response response = httpClient.newCall(request).execute();
        Log.d("MYTAG", "stop");
        return response.body().charStream();
    }

OkHttp版本:

compile 'com.squareup.okhttp:okhttp:2.3.0'

0 个答案:

没有答案