org.apache.http.NameValuePair不再适用于compileSdkVersion 23

por*_*der 3 apache android

我有一个帮助类来执行请求函数,我正在使用的一个方法如下.

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);
}
Run Code Online (Sandbox Code Playgroud)

如果我将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'
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*rte 7

在build.gradle上添加它

android {
    useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

小心使用Gradle 1.3.+


por*_*der 5

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

对于 API 23:

顶级 build.gradle - /build.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}
...
Run Code Online (Sandbox Code Playgroud)

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

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
    useLibrary 'org.apache.http.legacy'
    ...
}
Run Code Online (Sandbox Code Playgroud)

官方文档:
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'
 }
Run Code Online (Sandbox Code Playgroud)