Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

square - okhttp - OkHttpClient cannot be resolved

Tags:

java

android

Currently I am working on the Stormy Treehouse Android app. I want to use the square OkHttpClient but I get an error "cannot resolve symbol okhttp3" when importing the class : import com.squareup.okhttp3.OkHttpClient;. Replacing okhttp3 with okhttp does not fix the error. Thanks for you help.

MainActivity.java

package com.trainerworkout.stormy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

// cannot resolve symbol 'okhttp3'
import com.squareup.okhttp3.OkHttpClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String apiKey = "f9d033c3cdeab16b95cecdeb6721a093";
        double latitude = 45.5124;
        double longitude = -73.5547;
        String forecastUrl = "https://api.forecast.io/forecast/" + apiKey + "/" + latitude + "," + longitude;
        // Cannot resolve symbol
        OkHttpClient client = new OkHttpClient();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    } // onCreate()
} // MainActivity

build.gradle (Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.trainerworkout.stormy"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.squareup.okhttp3:okhttp:3.0.1'
}
like image 878
charlesfranciscodev Avatar asked Jan 15 '16 17:01

charlesfranciscodev


People also ask

What is Okhttpclient?

OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features, such as connection pooling (if HTTP/2 isn't available), transparent GZIP compression, and response caching, to avoid the network completely for repeated requests.

What is OkHttp library in Android?

OkHttp is a third-party library developed by Square for sending and receive HTTP-based network requests. It is built on top of the Okio library, which tries to be more efficient about reading and writing data than the standard Java I/O libraries by creating a shared memory pool.

Where is OkHttp used?

As of Android 5.0, OkHttp is part of the Android platform and is used for all HTTP calls.


2 Answers

Try Removing your import, and just go to this line

OkHttpClient client = new OkHttpClient();

move your cursor to OkHttpClient and press Alt+Enter, you will see 2 classes to choose import from as you can see in this image:

1

So basically there are two types of imports available

1). import com.squareup.okhttp.OkHttpClient;

2). import okhttp3.OkHttpClient;

and yours is matching none of them, Please try to import the correct one, depending upon your requirements.

like image 114
Mukesh Rana Avatar answered Sep 17 '22 07:09

Mukesh Rana


I too faced same problem, try changing your dependencies in the gradle as following.

**

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/okhttp-3.4.2.jar')
compile 'com.squareup.okhttp3:okhttp:3.4.2'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'

}

**

like image 38
prashant Avatar answered Sep 20 '22 07:09

prashant