日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android volley 上传图片 和参数,android Volley 上传文件上传图片

發布時間:2024/1/23 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android volley 上传图片 和参数,android Volley 上传文件上传图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android volley 實現上傳文件功能

Volley不解釋了吧, android 官方的一個網絡請求庫.

源代碼的地址在:

git@github.com:com314159/VolleyMultiPartRequest.git

上面的是ssh

下面的是http地址

https://github.com/com314159/VolleyMultiPartRequest

是根據

https://github.com/smanikandan14/Volley-demo

這位大神修改而來的, 但是那位大神的代碼有bug, 上傳文件不成功.

注: 我的demo里面還集成了okhttp, 不需要的同學不用理這個類即可

實現方法:

1.添加三個jar包,

httpcore-4.3.2.jar

httpclient-4.3.5.jar

httpmime-4.3.5.jar

2.實現MultiPartStack

package com.example.volleytest;

import java.io.File;

import java.io.IOException;

import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpDelete;

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPatch;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.client.methods.HttpUriRequest;

import org.apache.http.entity.ByteArrayEntity;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.mime.HttpMultipartMode;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.HttpConnectionParams;

import org.apache.http.params.HttpParams;

import org.apache.http.protocol.HTTP;

import com.android.volley.AuthFailureError;

import com.android.volley.Request;

import com.android.volley.Request.Method;

import com.android.volley.toolbox.HurlStack;

/**

* @author ZhiCheng Guo

* @version 2014年10月7日 上午11:00:52 這個Stack用于上傳文件, 如果沒有這個Stack, 則上傳文件不成功

*/

public class MultiPartStack extends HurlStack {

@SuppressWarnings("unused")

private static final String TAG = MultiPartStack.class.getSimpleName();

private final static String HEADER_CONTENT_TYPE = "Content-Type";

@Override

public HttpResponse performRequest(Request> request,

Map additionalHeaders) throws IOException, AuthFailureError {

if(!(request instanceof MultiPartRequest)) {

return super.performRequest(request, additionalHeaders);

}

else {

return performMultiPartRequest(request, additionalHeaders);

}

}

private static void addHeaders(HttpUriRequest httpRequest, Map headers) {

for (String key : headers.keySet()) {

httpRequest.setHeader(key, headers.get(key));

}

}

public HttpResponse performMultiPartRequest(Request> request,

Map additionalHeaders) throws IOException, AuthFailureError {

HttpUriRequest httpRequest = createMultiPartRequest(request, additionalHeaders);

addHeaders(httpRequest, additionalHeaders);

addHeaders(httpRequest, request.getHeaders());

HttpParams httpParams = httpRequest.getParams();

int timeoutMs = request.getTimeoutMs();

// TODO: Reevaluate this connection timeout based on more wide-scale

// data collection and possibly different for wifi vs. 3G.

HttpConnectionParams.setConnectionTimeout(httpParams, 5000);

HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);

/* Make a thread safe connection manager for the client */

HttpClient httpClient = new DefaultHttpClient(httpParams);

return httpClient.execute(httpRequest);

}

static HttpUriRequest createMultiPartRequest(Request> request,

Map additionalHeaders) throws AuthFailureError {

switch (request.getMethod()) {

case Method.DEPRECATED_GET_OR_POST: {

// This is the deprecated way that needs to be handled for backwards compatibility.

// If the request's post body is null, then the assumption is that the request is

// GET. Otherwise, it is assumed that the request is a POST.

byte[] postBody = request.getBody();

if (postBody != null) {

HttpPost postRequest = new HttpPost(request.getUrl());

if(request.getBodyContentType() != null)

postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());

HttpEntity entity;

entity = new ByteArrayEntity(postBody);

postRequest.setEntity(entity);

return postRequest;

} else {

return new HttpGet(request.getUrl());

}

}

case Method.GET:

return new HttpGet(request.getUrl());

case Method.DELETE:

return new HttpDelete(request.getUrl());

case Method.POST: {

HttpPost postRequest = new HttpPost(request.getUrl());

postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());

setMultiPartBody(postRequest,request);

return postRequest;

}

case Method.PUT: {

HttpPut putRequest = new HttpPut(request.getUrl());

if(request.getBodyContentType() != null)

putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());

setMultiPartBody(putRequest,request);

return putRequest;

}

// Added in source code of Volley libray.

case Method.PATCH: {

HttpPatch patchRequest = new HttpPatch(request.getUrl());

if(request.getBodyContentType() != null)

patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());

return patchRequest;

}

default:

throw new IllegalStateException("Unknown request method.");

}

}

/**

* If Request is MultiPartRequest type, then set MultipartEntity in the

* httpRequest object.

*

* @param httpRequest

* @param request

* @throws AuthFailureError

*/

private static void setMultiPartBody(HttpEntityEnclosingRequestBase httpRequest,

Request> request) throws AuthFailureError {

// Return if Request is not MultiPartRequest

if (!(request instanceof MultiPartRequest)) {

return;

}

// MultipartEntity multipartEntity = new

// MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

/* example for setting a HttpMultipartMode */

builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

// Iterate the fileUploads

Map fileUpload = ((MultiPartRequest) request).getFileUploads();

for (Map.Entry entry : fileUpload.entrySet()) {

builder.addPart(((String) entry.getKey()), new FileBody((File) entry.getValue()));

}

ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);

// Iterate the stringUploads

Map stringUpload = ((MultiPartRequest) request).getStringUploads();

for (Map.Entry entry : stringUpload.entrySet()) {

try {

builder.addPart(((String) entry.getKey()),

new StringBody((String) entry.getValue(), contentType));

} catch (Exception e) {

e.printStackTrace();

}

}

httpRequest.setEntity(builder.build());

}

}

總結

以上是生活随笔為你收集整理的android volley 上传图片 和参数,android Volley 上传文件上传图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。