Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android http connecting with OkHttp Dont work

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.

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$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)

Here is the code sample I am trying to use. Its from a tutorial online (teamtreehouse.com) Java code:

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);
                    }
                }
            });
like image 515
Thiago Avatar asked Jan 24 '15 10:01

Thiago


People also ask

Is OkHttp blocked?

OkHttp Overview At a high level, the client is designed for both blocking synchronous calls and nonblocking asynchronous calls. OkHttp supports Android 2.3 and above.

How do I use OkHttp on Android?

First, we create the OkHttpClient instance. Then, we need to create the request via the Request object and its Builder class. Next step is to pass the request in parameter of the newcall method of the OkHttpClient instance created. OkHttp supports asynchronous and synchronous calls.

Which is better OkHttp or retrofit?

OkHttp is a pure HTTP/SPDY client responsible for any low-level network operations, caching, requests and responses manipulation. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. Retrofit is strongly coupled with OkHttp and makes intensive use of it.

Why do we use OkHttp in Android?

Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.


1 Answers

OkHttp needs Okio, which OkHttp uses for fast I/O and resizable buffers. You can download Okio (latest JAR) here.

or

Android gradle : compile 'com.squareup.okio:okio:1.6.0'

Maven

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

for version checks; github okio

like image 164
Aldo Avatar answered Sep 30 '22 17:09

Aldo