尝试资源要求API级别19(OkHttp) [英] Try-with-resources requires API level 19 (OkHttp)

查看:208
本文介绍了尝试资源要求API级别19(OkHttp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OkHttp来获取Web服务器响应. 我当前的minSdkVersion 15.

I am trying to get web server response with OkHttp. My current minSdkVersion 15.

我的代码是

    @Override
    protected String doInBackground(String... strings) {

        GetDataFromUrl getData = new GetDataFromUrl();
        String response = null;
        try {
                response = getData.run(URL);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

还有

String run(String url) throws IOException {
Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

我在try (Response response = client.newCall(request).execute())行收到警告.

说"Try-with-resources requires API level 19 (current min is 15).

我知道,如果我将最低API级别更改为19,它将可以正常工作.但是我必须支持至少15个API级别.

I know that if I change the minimum API level to 19, it'll work fine. But I have to support min 15 API level.

有什么解决办法吗?

推荐答案

解决方案是不要使用try-with-resources,除非您可以将最小API级别设置为19.

The solution is to not use try-with-resources unless you can set your min API level to 19. So instead of this:

try (Response response = client.newCall(request).execute()) {
    return response.body().string();
}

您应该这样做:

Response response = null;
try {
    response = client.newCall(request).execute();
    return response.body().string();
} finally {
    if (response != null) {
        response.close();
    }
}

Java语言规范,第14.20.3.1节提供了与您一样的基本try-with-resources语句(一个不带任何catchfinally块的语句)的稍有不同(但在这种情况下,功能相同)有:

The Java Language Specification, Section 14.20.3.1 provides a slightly different (but, in this case, functionally identical) equivalent to a basic try-with-resources statement (one without any catch or finally blocks) like you have:

{
    final Response response = client.newCall(request).execute();
    Throwable primaryExc = null;

    try {
        return response.body().string();
    } catch (Throwable t) {
        primaryExc = t;
        throw t;
    } finally {
        if (response != null) {
            if (primaryExc != null) {
                try {
                    response.close();
                } catch (Throwable suppressed) {
                    primaryExc.addSuppressed(suppressed);
                }
            } else {
                response.close();
            }
        }
    }
}

这有两个效果.首先,它使response变量位于等效块的本地. (我的建议是在try语句完成后使它可见,这可能是不希望的.)更重要的是,它具有抑制关闭资源时引发的任何异常的作用.也就是说,如果原始try块的主体引发异常,则调用代码将看到该异常,而不是close()引发的异常. (close()抛出的异常仍可以通过实际抛出的异常的getSuppressed()方法获得.)您不需要这个更复杂的版本,因为(据我从API文档所知道的)不会引发异常.

This has two effects. First, it makes the response variable local to the equivalent block. (My suggestion made it visible after the try statement finishes, which may be undesirable.) More importantly, it has the effect of suppressing any exception thrown while closing the resource. That is, if the body of the original try block throws an exception, invoking code will see that instead of the exception thrown by the close(). (The exception thrown by the close() would still be available via the getSuppressed() method of the exception actually thrown.) You don't need this more complicated version because (as far as I can tell from the API docs) Response.close() doesn't throw exceptions.

这篇关于尝试资源要求API级别19(OkHttp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆