用凌空上传多张图片?

Uploading multiple images with volley?

我在 SO 和其他 tuts 中经历了很多 post,因为 well.But 我无法获得任何最新的官方或其他 post 不包含任何已弃用的内容使用 volley.I 上传多张图片的代码在新的 android M 中了解了 Apache HTTP 客户端删除和相关内容,并且更喜欢在下面使用。

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

所以,谁能帮我上传多张图片,使用新的已弃用的 less volley class?

目前,Volley 库(Google 和 mcxiaoke 的库)仍然在其许多 类 中使用 Apache 的库。如果您仍想在没有任何 Apache 依赖项 的情况下使用 Volley ,您需要将其作为项目中的模块使用并修改其源代码文件。

你可以参考my GitHub sample code ,那里你会发现我定制了一些类比如NetworkResponseHttpHeaderParserBasicNetworkHurlStack, Volley... 对于多部分请求,请使用 MultipartActivity.java 文件。

您将看到 build.gradle 文件内容:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.volleynoapache"
        minSdkVersion 16
        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'
}

另一种很好的替代方案,恕我直言,正在使用 OkHttp,我在 GitHub 也有一个有效的示例代码,请看一下。

希望对您有所帮助!

您可以使用来自 here 的最新版本的 volley。这是一个非官方镜像,修复了一些小错误,源代码将定期与官方 volley 存储库同步。

为Gradle

compile 'com.mcxiaoke.volley:library:1.0.19' 

或者你可以从here

下载编译好的版本

现在,您可以使用下面附带的 class 在 org.apache.http.entity.mime 中的 MultipartEntityBuilder 的帮助下使用 volley 发出多部分请求,而无需任何弃用代码。

CustomMultipartRequest.java

示例用法

//Auth header
Map<String, String> mHeaderPart= new HashMap<>();
mHeaderPart.put("Content-type", "multipart/form-data;");
mHeaderPart.put("access_token", accessToken);

//File part
Map<String, File> mFilePartData= new HashMap<>();
mFilePartData.put("file", new File(mFilePath));
mFilePartData.put("file", new File(mFilePath));

//String part
Map<String, String> mStringPart= new HashMap<>();
mStringPart.put("profile_id","1");
mStringPart.put("imageType", "ProfileImage");

CustomMultipartRequest mCustomRequest = new CustomMultipartRequest(method, mContext, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                listener.onResponse(jsonObject);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.onErrorResponse(volleyError);
            }
        }, mFilePartData, mStringPart, mHeaderPart);

您可以使用 httpmime-4.3.5.jarhttpcore-4.3.2.jar 来访问 MultipartEntityBuilder 和其他用于发出请求的方法,或者在您的 gradle 中添加以下内容如果您的目标 API 23 岁及以上。

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

我使用提到的罐子的任何方式,它在 Android M 中也很有魅力。

更新

请注意,com.mcxiaoke.volley:library:1.0.19 已弃用且不再维护,请使用 jCenter 的官方版本。

compile 'com.android.volley:volley:1.0.0'