Failed to import com.squareup.okhttp.OkHttpClient;

I am working on an Android studio and getting some data from the Internet. I tried using OkHttpClient and also added jars to my project folder, but still I can not import it.

Here can't resolve symbol okhttp . I tried some solution but could not solve the problem. Here is my build.gradle file

 apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.example.app" minSdkVersion 14 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']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.google.android.gms:play-services:8.4.0' // compile files('libs/okhttp-3.0.1.jar') // compile files('libs/okio-1.6.0.jar') } 
+8
source share
5 answers

Gradle should have a line like this

 implementation 'com.squareup.okhttp3:okhttp:3.0.1' 

and this is how you import it

 import okhttp3.OkHttpClient; 

Because OkHttpClient been moved from the com.squareup.okhttp package to okhttp3 in the latest version.

More information here and here.

+29
source

You need to add the following libraries:

 compile 'com.squareup.okhttp:okhttp:2.2.0' compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' 
+2
source

Download and import the gradle gradle handle for you:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.google.android.gms:play-services:8.4.0' compile 'com.squareup.okhttp3:okhttp:3.0.1' } 
+1
source

I see that you tried to compile with the jar, which you manually placed in your libs folder. Is there a reason for this (for example, do you need this particular version of the library, etc.)? If you have not noticed, the lines are commented out - this means that they will not be processed and therefore will not be added to your application.

Possible solutions:

  • Uncomment lines
  • Try adding this instead of dependencies :

     compile 'com.squareup.okhttp3:okhttp:3.0.1' 

You can add it right below

 compile 'com.google.android.gms:play-services:8.4.0' 

Source: https://github.com/square/okhttp

0
source
 android { compileSdkVersion 25 buildToolsVersion '25.0.2' useLibrary 'org.apache.http.legacy' ... } dependencies { ... compile 'com.squareup.okhttp3:okhttp:3.4.1' } 

And change your import from com.squareup.okhttp.OkHttpClient to

 import okhttp3.OkHttpClient; 
0
source

Source: https://habr.com/ru/post/1240996/


All Articles