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

前端 未结 5 2077
既然无缘
既然无缘 2020-12-10 13:46

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 t

相关标签:
5条回答
  • 2020-12-10 14:19

    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

    0 讨论(0)
  • 2020-12-10 14:22

    You need to add the following libraries:

    compile 'com.squareup.okhttp:okhttp:2.2.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    
    0 讨论(0)
  • 2020-12-10 14:22

    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. Uncomment the lines
    2. Try adding this to your dependencies instead:

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

    You can add it right under

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

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

    0 讨论(0)
  • 2020-12-10 14:23

    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'
    }
    
    0 讨论(0)
  • 2020-12-10 14:38
    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 讨论(0)
提交回复
热议问题