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.
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
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'
}
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.
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.
As of Android 5.0, OkHttp is part of the Android platform and is used for all HTTP calls.
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:
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.
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'
}
**
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With