无法解析符号NameValuePair

24

在我的项目中出现了一个错误:

Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'。

然后我尝试使用以下方法进行修复:

compileSdkVersion 23

但是我遇到了错误:

无法解析符号NameValuePair android

如何修复这个错误?

7个回答

40

如果您想在最新的API级别下在Android Studio中使用NameValuePairBasicNameValuePair,请按照以下步骤进行:

  • 打开build.gradle(Module)文件并复制以下依赖项:

implementation 'com.google.http-client:google-http-client-android:+'

implementation 'com.google.api-client:google-api-client-android:+'

implementation 'com.google.api-client:google-api-client-gson:+'
  • buildToolsVersion 下方加入 useLibrary 'org.apache.http.legacy',如下所示:

  • android {
    useLibrary 'org.apache.http.legacy'}
    

    就这些了。现在只需同步 Gradle 文件即可。

    注意:在依赖项中,我建议使用最新版本的库,而不是使用 + 符号。


    为什么需要使用Gson? - natinusala
    1
    只需添加"useLibrary"一行代码,就可以解决我的问题。谢谢! - Oscar Josue Alvrez

    14

    NameValuePairorg.apache包的一部分,该包在Android 22中被弃用并在Android M中被删除,而您正在编译的版本正是该版本。有趣的是,NameValuePair文档也无法访问。


    寻找替代品?你用它做什么? - Blackbelt
    1
    我的所有项目都使用 org.apache.http 中的 NameValuePairBasicNameValuePair - g8214435
    然后你必须将其迁移到URLConnection或其他HTTP客户端。 - Blackbelt
    那么如何创建一个兼容所有版本的Android类(从Android 2.3到N)? - Elshan
    使用 HttpUrlConnection @devopsEMK - Blackbelt

    11

    今天我也遇到了同样的问题。我通过在我的 build.gradle 中进行两个更改来解决它。

     1)   android {
            useLibrary 'org.apache.http.legacy'
                  }
    
    2)  compile 'org.apache.httpcomponents:httpcore:4.4.1'
        compile 'org.apache.httpcomponents:httpclient:4.5'
    

    我的最终 build.gradle 文件:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "com.corouter"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    android {
        useLibrary 'org.apache.http.legacy'
    }
    dependencies
            {
                compile fileTree(include: ['*.jar'], dir: 'libs')
                testCompile 'junit:junit:4.12'
                compile 'com.android.support:appcompat-v7:23.2.0'
                compile 'com.android.support:recyclerview-v7:23.0.+'
                compile 'org.apache.httpcomponents:httpcore:4.4.1'
                compile 'org.apache.httpcomponents:httpclient:4.5'
    
            }
    

    非常感谢,你的解决方案有效! - Sourav Das
    你好,但我建议你使用Volley库,因为这些已经过时了... - Däñish Shärmà
    嗯,我一定会去看看的。 - Sourav Das
    太好了。如果你需要任何帮助...随时问我。 - Däñish Shärmà

    5
    添加这个:
    compile'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    

    在基于应用程序的build.gradle中它会被捕捉到。

    对我有用。谢谢!简单的一行解决方案。 :) - Murtuza K

    3

    嵌套类将有效:

    private class NameValuePair {
            private String mName;
            private String mValue;
    
            public NameValuePair(String name, String value) {
                mName = name;
                mValue = value;
            }
    
            public String getName() {
                return mName;
            }
    
            public String getValue() {
                return mValue;
            }
        };
    

    2

    我也遇到了同样的问题。为了解决它,我使用了以下方法:

    import cz.msebera.android.httpclient.NameValuePair;
    import cz.msebera.android.httpclient.message.BasicNameValuePair;
    

    并且还添加了。
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile "cz.msebera.android:httpclient:4.4.1.1"
    compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
    

    添加到应用程序/Gradle依赖项中。希望这有所帮助。


    0

    遇到了同样的问题

    解决方法如下:

    1. 在依赖项中添加以下内容:

    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

    1. 在defaultConfig中的targetSdkVersion下面添加以下内容

    useLibrary 'org.apache.http.legacy'


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接