android square - okhttp -无法解析OkHttpClient

w8biq8rn  于 2022-12-31  发布在  Android
关注(0)|答案(3)|浏览(382)

目前我正在处理Stormy Treehouse Android app。我想使用square OkHttpClient,但在导入类时收到错误"无法解析符号okhttp3":import com.squareup.okhttp3.OkHttpClient;。将okhttp3替换为okhttp无法修复错误。感谢您的帮助。

MainActivity.java

...

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 = "...";
        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(模块:应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "..."
        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'
}
oogrdqng

oogrdqng1#

尝试删除导入,然后转到此行

OkHttpClient client = new OkHttpClient();

将光标移动到OkHttpClient并按Alt+Enter,您将看到2个类,可以从中选择导入,如下图所示:

基本上有两种类型的导入可用
1). import com.squareup.okhttp.OkHttpClient;
2). import okhttp3.OkHttpClient;
而您的文件与它们都不匹配,请根据您的要求尝试导入正确的文件。

zzwlnbp8

zzwlnbp82#

我也遇到了同样的问题,请尝试在gradle中更改您的依赖项,如下所示。

**

相关性{

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'

}

**

2izufjch

2izufjch3#

您必须在build.gradle(app)中添加以下两个依赖项:
编译“com.squareup.okhttp:okhttp:2.2.0”
编译“com.squareup.okhttp:okhttp-网址连接:2.0.0”
之后,您将能够完美地安装您的应用程序。

相关问题