java Android http 连接 OkHttp 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28124587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android http connecting with OkHttp Dont work
提问by Joolah
I am trying to use OkHttp but it keep crashing. Can someone have a quick look and see if you know whats happening. Thank you.
我正在尝试使用 OkHttp,但它一直崩溃。有人可以快速查看一下,看看您是否知道发生了什么。谢谢你。
Log cat:
日志猫:
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.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.access0(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)
Here is the code sample I am trying to use. Its from a tutorial online (teamtreehouse.com) Java code:
这是我尝试使用的代码示例。它来自在线教程(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);
}
}
});
回答by Vinc
OkHttp needs Okio, which OkHttp uses for fast I/O and resizable buffers. You can download Okio (latest JAR) here.
OkHttp 需要 Okio,OkHttp 使用它来实现快速 I/O 和可调整大小的缓冲区。你可以在这里下载 Okio(最新的 JAR)。
or
或者
Android gradle : compile 'com.squareup.okio:okio:1.6.0'
安卓梯度: compile 'com.squareup.okio:okio:1.6.0'
Maven
马文
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
回答by Tejas Sherdiwala
There seems to be a dependency on the 1.0 library of OkHTTP. It is a known report. You may follow it on the link below:
似乎依赖于 OkHTTP 的 1.0 库。这是一个众所周知的报告。您可以通过以下链接关注它:
https://github.com/square/okhttp/issues/870
https://github.com/square/okhttp/issues/870
You should also include the library 1.0in the project libs.
您还应该在项目库中包含库 1.0。