在网上找的关于Volley上传文件的方法,(当然可以用来传图片)
转载自http://blog.csdn.net/com314159/article/details/39853865
说的很详细,我拿来尝试了一下,上传图片挺好用的
鉴于已经说的很详细了,我就不再累述,直接贴我实现的代码。
别忘了添加原文作者所说的三个文件MultiPartRequest.java,MultiPartStack.java,MultiPartStringRequest.java
private static RequestQueue mSingleQueue;
mSingleQueue = Volley.newRequestQueue(this, new MultiPartStack());实现的方法:
private void UploadFile(){ // showProgress(); Map最后需要注意的一点是:在Android Studio中添加三个jar包时,可能会发生错误,需要在build.gradle添加如下代码:, File> files = new HashMap , File>(); files.put("image1", new File( "/sdcard/a1.jpg")); files.put("image2", new File( "/sdcard/a2.jpg")); files.put("image3", new File( "/sdcard/a3.jpg")); Map , String> params = new HashMap , String>(); params.put("token", "DJrlPbpJQs21rv1lP41yiA=="); String uri = "http://192.168.247.1:9060/Uploadfile/uploadServlet"; addPutUploadFileRequest( uri, files, params, mResonseListenerString, mErrorListener, null); } Response.Listener mResonseListenerString = new Response.Listener () { @Override public void onResponse(String response) { //返回的数据 Toast.makeText(MainActivity.this,"返回的数据:"+response,Toast.LENGTH_SHORT).show(); } }; Response.ErrorListener mErrorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (error != null) { if (error.networkResponse != null) Log.i(TAG, " error " + new String(error.networkResponse.data)); } } }; public static void addPutUploadFileRequest(final String url, final Map , File> files, final Map , String> params, final Response.Listener responseListener, final Response.ErrorListener errorListener, final Object tag) { if (null == url || null == responseListener) { return; } MultiPartStringRequest multiPartRequest = new MultiPartStringRequest( Request.Method.POST, url, responseListener, errorListener) { @Override public Map , File> getFileUploads() { return files; } @Override public Map , String> getStringUploads() { return params; } }; // hiddenProgress(); mSingleQueue.add(multiPartRequest); }
packagingOptions { exclude 'META-INF/DEPENDENCIES.txt' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/notice.txt' exclude 'META-INF/license.txt' exclude 'META-INF/dependencies.txt' exclude 'META-INF/LGPL2.1' }添加后的结果是这样的:
android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "dao.d.volley" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } packagingOptions { exclude 'META-INF/DEPENDENCIES.txt' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/notice.txt' exclude 'META-INF/license.txt' exclude 'META-INF/dependencies.txt' exclude 'META-INF/LGPL2.1' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
最后,附上上一篇写的Volley的GET,POST网络请求和这次的源码,以及服务端的源码。下载地址