gpt4 book ai didi

java - Android http 连接 OkHttp 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:57 26 4
gpt4 key购买 nike

我正在尝试使用 OkHttp,但它总是崩溃。有人可以快速浏览一下,看看您是否知道发生了什么。谢谢。

日志猫:

01-24 08:34:46.952: E/AndroidRuntime(31953): FATAL EXCEPTION: OkHttp Dispatcher
01-24 08:34:46.952: E/AndroidRuntime(31953): java.lang.NoClassDefFoundError: okio.Okio
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.internal.http.HttpConnection.<init>(HttpConnection.java:87)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:272)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Connection.connect(Connection.java:158)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:174)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:120)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:131)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:312)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:235)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Call.getResponse(Call.java:262)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:219)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:192)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Call.access$100(Call.java:34)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:156)
01-24 08:34:46.952: E/AndroidRuntime(31953): at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
01-24 08:34:46.952: E/AndroidRuntime(31953): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-24 08:34:46.952: E/AndroidRuntime(31953): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-24 08:34:46.952: E/AndroidRuntime(31953): at java.lang.Thread.run(Thread.java:841)

这是我尝试使用的代码示例。它来自在线教程 (teamtreehouse.com)Java代码:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();

Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
alertUserAboutError();
}

@Override
public void onResponse(Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});

try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUserAboutError();
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});

最佳答案

OkHttp 需要 Okio,OkHttp 将其用于快速 I/O 和可调整大小的缓冲区。您可以下载 Okio(最新的 JAR)here .

Android gradle:编译'com.squareup.okio:okio:1.6.0'

专家

<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>

for version checks; github okio

关于java - Android http 连接 OkHttp 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28124587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com