Manish Singla
Manish Singla

Reputation: 409

not able to import com.squareup.okhttp.OkHttpClient;

I am working on android studio and fetching some data from the web. I tried using OkHttpClient and also added jars to my project folder but still i am unable to import it.

It shows can't resolve symbol okhttp. I tried some solution but unable to 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')


}

Upvotes: 19

Views: 59543

Answers (8)

Thirumalvalavan
Thirumalvalavan

Reputation: 2768

At my case, I have used 2 libraries,

implementation ("com.squareup.retrofit2:retrofit:2.9.0")

&

implementation ("com.squareup.okhttp3:okhttps:3.4.1")

due to that reason, the issue occurred.

OkHttp dependency is already shipped with Retrofit 2 dependency.

So I have commented below library, the issue was gone.

//implementation ("com.squareup.okhttp3:okhttps:3.4.1") // No need this library.

Upvotes: 0

nbokmans
nbokmans

Reputation: 5747

I see you are/were trying to compile with a jar you manually put in your libs folder. Is there a reason for this (such as you need that specific version of the library etc.)? In case you didn't notice, the lines are commented out - that means they will not be processed and thus not added to your application.

Possible solutions:

  1. Try adding this latest version to your dependencies instead:

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

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

Upvotes: 2

fgadev
fgadev

Reputation: 21

In build.gradle import as api, e.g.

dependencies {
api 'com.squareup.okhttp3:okhttp:3.11.0'

see this note https://github.com/dropbox/dropbox-sdk-java#when-i-use-okhttp3requestor-in-dbxrequestconfig-i-get-errors-like-class-file-for-okhttp3okhttpclient-not-found

Upvotes: 0

venkatesh
venkatesh

Reputation: 329

use the latest version of it!

In my case, tried all the possibilities but after updating to the latest version, the issue is resolved.

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

Upvotes: 2

Rohit5k2
Rohit5k2

Reputation: 18112

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 has been moved from package com.squareup.okhttp to okhttp3 in the last version.

More details are here and here

Upvotes: 38

livs
livs

Reputation: 145

You need to add the following libraries:

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

Upvotes: 2

Linh
Linh

Reputation: 60923

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;

Upvotes: 0

Jörn Buitink
Jörn Buitink

Reputation: 2916

Let gradle handle download and import 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'
}

Upvotes: 1

Related Questions