【问题标题】:org.apache.http.NameValuePair no longer available with compileSdkVersion 23org.apache.http.NameValuePair 不再适用于 compileSdkVersion 23
【发布时间】:2015-10-05 22:18:34
【问题描述】:

我有一个请求函数的辅助类,我使用的一种方法如下。

private String buildQueryString(String url, List<NameValuePair> params) throws IOException {
    StringBuilder sb = new StringBuilder();
    if (params == null) return url;

    for (NameValuePair param : params) {
        sb.append(urlEncode(param.getName()));
        sb.append("=");
        sb.append(urlEncode(param.getValue()));
        sb.append("&");
    }

    return url + "?" + sb.substring(0, sb.length() - 1);
}

如果我将我的 gradle 更新为 compileSdkVersion 23,则导入 org.apache.http.NameValuePair 不再存在。我很想知道替代品是什么?提前致谢!

EDIT -posting gradle- 另外,我使用的是 gradle-2.4

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    signingConfigs {
        debug {
            storeFile file('keystore/debug/debug.keystore')
        }
    }

    compileSdkVersion 23
    buildToolsVersion "21.1.2"
    useLibrary 'org.apache.http.legacy'

    // jenkins setup
    def versionPropsFile = file('version.properties')
    def code = 1;
    if (versionPropsFile.canRead() && versionPropsFile.exists()) {
        def Properties versionProps = new Properties()

        versionProps.load(new FileInputStream(versionPropsFile))
        List<String> runTasks = gradle.startParameter.getTaskNames();
        def value = 0
        for (String item : runTasks) {
            if (item.contains("assembleRelease")) {
                value = 1;
            }
        }
        code = Integer.parseInt(versionProps['VERSION_CODE']).intValue() + value
        versionProps['VERSION_CODE'] = code.toString()
        versionProps.store(versionPropsFile.newWriter(), null)
    } else {
        throw new GradleException("Could not read version.properties!")
    }

    // construct version name
    def versionMajor = 2
    def versionMinor = 3
    def versionPatch = 9

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
        versionCode code
        signingConfig signingConfigs.debug

        // enabling multidex support
        multiDexEnabled true
    }

    buildTypes {
        stage {
              <content removed>
        }
        debug {
              <content removed>
        }
        release {
              <content removed>
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }


dependencies {
    <bunch of dependencies>
    compile 'com.android.support:support-v4:23.0.1'
}

【问题讨论】:

    标签: android apache


    【解决方案1】:

    在你的 build.gradle 中添加这个

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

    org.apache 库已从 api 22 中弃用,并在 api 23 中被淘汰。

    buildToolsVersion '21.1.2' 更改为buildToolsVersion '23.0.1'

    或者你应该在你的依赖中添加这个:

    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.cli‌​ent:4.1.2'
    

    小心使用 Gradle 1.3.+

    【讨论】:

    • 在 android 中添加会导致错误,“未找到 Gradle DSL 方法:'useLibrary()'。
    • 请发布您的完整 gradle 文件
    • 您需要将 buildToolsVersion "21.1.2" 更改为 buildToolsVersion '23.0.1'
    • 想通了!有几种方法,你是对的,但我可以通过添加 compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client 来实现它: 4.1.2'
    • 大声笑,我们有同样的想法。我刚刚用我的编辑更新了我的答案。感谢您的帮助。
    【解决方案2】:

    为了详细说明这个问题,有两种方法可以解决那些面临相同冲突的问题。正如@Michele 指出的那样,useLibrary 'org.apache.http.legacy' 可以使用,但您必须记住也要更新 gradle。

    对于 API 23:

    顶级 build.gradle - /build.gradle

    buildscript {
        ...
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.1'
        }
    }
    ...
    

    模块特定的 build.gradle - /app/build.gradle

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.0"
        useLibrary 'org.apache.http.legacy'
        ...
    }
    

    官方文档:
    https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

    最新的 android gradle 插件更新日志:http://tools.android.com/tech-docs/new-build-system

    解决此问题的第二种方法是将以下内容添加到依赖项中

     dependencies {
        compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2016-01-03
      • 1970-01-01
      • 2014-05-17
      • 2019-10-23
      • 2018-02-13
      相关资源
      最近更新 更多