charlesfranciscodev
charlesfranciscodev

Reputation: 313

square - okhttp - OkHttpClient cannot be resolved

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

...

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 = "...";
        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 "..."
        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'
}

Upvotes: 9

Views: 42103

Answers (3)

Prabhu.d
Prabhu.d

Reputation: 11

You have to add these two dependencies in your build.gradle(app) :

compile 'com.squareup.okhttp:okhttp:2.2.0'

compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'

After that you will be able to install your application perfectly.

Upvotes: 0

Mukesh Rana
Mukesh Rana

Reputation: 4091

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.

Upvotes: 17

prashant
prashant

Reputation: 3318

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'

}

**

Upvotes: 1

Related Questions