【发布时间】:2020-10-13 11:45:06
【问题描述】:
我正在使用 OKHTTP 3.14 并且我的设备有 API 24 (android 7.0),它支持吗? 因为在更高级别的设备中发出 http 请求时,它工作得非常好,但在 API 级别 24 的设备中却不行。
编辑: 当我在下面运行这个函数时,它可以在比 24 更新的 API 中运行,但是当我在 API 级别 24 的旧设备上运行它时,它会引发异常:
private String get_OKHttp3Response(String _uri) {
OkHttpClient httpClient = new OkHttpClient();
String strResp = "";
try {
Request request = new Request.Builder().url(_uri).build();
Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
strResp = response.body().string();
if (!strResp.equalsIgnoreCase("")) {
return strResp;
} else {
return "";
}
} catch (Exception e) {
System.out.println( "get_OKHttp3Response Exception: " + e.getMessage() );
return "";
}
};
我在抛出异常时收到此消息:
Exception: null
【问题讨论】:
-
OkHttp 适用于 Android 5.0+(API 级别 21+)和 Java 8+。 (来自 github 自述文件)github.com/square/okhttp。最新版本是
4.7.2,或许你可以尝试更新一下 -
您尚未指定遇到的错误,也未向我们展示相关代码。我已经在 Android 5 到 Android 11 上成功使用了 OkHttp 3.14。
-
@Michael 看到编辑后的问题,我在那里发布了代码。
-
请包含异常的堆栈跟踪。