Uploading multiple images with volley?
Asked Answered
N

2

21

I have gone through a lot of post in SO and other tuts as well.But i couldn't get any latest official or other post that doesn't contain any deprecated code for uploading multiple images using volley.I came to know Apache HTTP Client removal and related in new android M and preferred to use below instead.

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

So, can any one help me out for doing multiple image upload with new updated deprecated less volley class ?

Newspaperman answered 14/12, 2015 at 5:10 Comment(1)
okhttp. take a look here: github.com/square/okhttp/wiki/…Eichmann
G
4

You can use the latest version of volley from here.It's an unofficial mirror with some minor bug fix and the source code will synchronize periodically with the official volley repository.

for Gradle

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

or you can download the compiled version from here

Now you can use the below attached class for making multipart request using volley by the help of MultipartEntityBuilder in org.apache.http.entity.mime without having any deprecated code.

CustomMultipartRequest.java

Sample usage

//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);

Either you can use httpmime-4.3.5.jar and httpcore-4.3.2.jar for getting access of MultipartEntityBuilder and other methods which is used to make the request or add the following in your gradle if your targeting API 23 and above.

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

Any way I'm using the mentioned jar's and it's works like a charm in Android M also.

Update

Please note, com.mcxiaoke.volley:library:1.0.19 deprecated and no longer being maintained, please use official version from jCenter.

compile 'com.android.volley:volley:1.0.0'
Galatia answered 17/12, 2015 at 4:48 Comment(4)
Thank you @Anoop M it's works without any deprecated code. :)Newspaperman
Actually, mcxiaoke's Volley still uses API22, you can see at github.com/mcxiaoke/android-volley/blob/master/… :)Vanmeter
Is it possible to upload several files with same key "file" ?Tiercel
As he explains in the answer update, you may use the official version of volley rather trying with the deprecated one.Newspaperman
V
2

At this moment, Volley library (both Google's and mcxiaoke's one) still uses Apache's library inside its many classes. If you still want to use Volley without any Apache dependency, you need to use it as a module inside your project and modify its source code file.

You can refer to my GitHub sample code , there you will find that I have customized some classes such as NetworkResponse, HttpHeaderParser, BasicNetwork, HurlStack, Volley... For multipart request, please use the MultipartActivity.java file.

You will see build.gradle file content:

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

Another good alternavite solution, IMHO, is using OkHttp, I have also had a working sample code at GitHub, please take a look.

Hope it helps!

Vanmeter answered 14/12, 2015 at 8:43 Comment(2)
Thanks for your help BNK.but actually i want to do with this using Apache dependency.Newspaperman
Oh! So what do you mean with deprecated less? If still want so, pls read my link at #32240677Vanmeter

© 2022 - 2024 — McMap. All rights reserved.