Java square - okhttp - OkHttpClient 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34816171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:00:52  来源:igfitidea点击:

square - okhttp - OkHttpClient cannot be resolved

javaandroid

提问by charlesfranciscodev

Currently I am working on the Stormy Treehouse Android app. I want to use the square OkHttpClientbut 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.

目前我正在开发Stormy Treehouse Android 应用程序。我想用方OkHttpClient但我得到一个错误“无法解析符号okhttp3”导入类时:import com.squareup.okhttp3.OkHttpClient;。用 okhttp 替换 okhttp3 不能修复错误。谢谢你的帮助。

MainActivity.java

主活动.java

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

build.gradle (Module:app)

build.gradle(模块:app)

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'
}

采纳答案by Mukesh Rana

Try Removing your import, and just go to this line

尝试删除您的导入,然后转到这一行

OkHttpClient client = new OkHttpClient();

move your cursor to OkHttpClientand press Alt+Enter, you will see 2 classes to choose import from as you can see in this image:

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

1

1

So basically there are two types of imports available

所以基本上有两种类型的进口可用

1). import com.squareup.okhttp.OkHttpClient;

1)。 import com.squareup.okhttp.OkHttpClient;

2). import okhttp3.OkHttpClient;

2)。 import okhttp3.OkHttpClient;

and yours is matching none of them, Please try to import the correct one, depending upon your requirements.

并且您的不匹配,请根据您的要求尝试导入正确的。

回答by prashant

I too faced same problem, try changing your dependencies in the gradle as following.

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

**

**

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'

}

}

**

**

回答by Prabhu.d

You have to add these two dependencies in your build.gradle(app) :

您必须在 build.gradle(app) 中添加这两个依赖项:

compile 'com.squareup.okhttp:okhttp:2.2.0'

编译'com.squareup.okhttp:okhttp:2.2.0'

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

编译'com.squareup.okhttp:okhttp-urlconnection:2.0.0'

After that you will be able to install your application perfectly.

之后,您将能够完美地安装您的应用程序。