charlesfranciscodev charlesfranciscodev - 17 days ago 10
Java Question

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



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'
}

Answer

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 http://prntscr.com/9qhbnk

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.