我正在尝试使用android中的volley将post参数作为用户名和图像发送为multiform数据。我正在创建正文以及发送参数,但似乎邮政服务的主体以某种方式覆盖了从服务器给出的参数。我使用自定义请求类,其中编写的代码如下:
public abstract class CustomJsonRequest<T> extends Request<T> {
................
@Override
protected Map<String, String> getParams() throws AuthFailureError {
// return super.getParams();
HashMap<String, String> map = new HashMap<>();
map.put("userName", requestParameters.get("userName"));
return map;
}
public byte[] getBody() throws AuthFailureError {
return createPostBody(requestParameters).getBytes();
}
private String createPostBody(Map<String, String> params) {
StringBuilder sbPost = new StringBuilder();
if (params != null) {
// for (String key : params.keySet()) {
if (params.get("image") != null) {
sbPost.append("\r\n" + "--" + BOUNDARY + "\r\n");
sbPost.append("Content-Disposition: form-data; name=\"" + "image" + "\"" + "\r\n\r\n");
sbPost.append(params.get("image"));
}
//}
}
return sbPost.toString();
}
}
在调试时我可以看到没有调用getParameters。我的目标是使用android中的volley发送带有图像的post参数作为其post体。在Ios中,它使用alamofire并通过以下代码成功上传。
func uploadImage(userImage: UIImage, completion: (result: RestResultType) -> Void) {
let parameters = ["userName": localData.getAccessToken()]
let url = domain + RestExt.imageUpload.rawValue
Alamofire.upload(.POST, url, multipartFormData: {
multipartFormData in
let imageData = UIImageJPEGRepresentation(userImage, 0.5)
if(imageData == nil) {
completion(result: .Error(e: method(code: "ImageConversionError",message: "")))
return
}
multipartFormData.appendBodyPart(data: imageData!, name: "image", fileName: "image.png", mimeType: "image/png")
for (key, value) in parameters {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
}, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON {
response in
switch response.result {
case .Success(let JSONResponse) :
let response = JSONResponse as! NSDictionary
let error = response.objectForKey("error") as? NSDictionary
if(error != nil) {
completion(result: .Error(e: method(JSONDictionary: error!)))
}
else {
completion(result: .Success(r: response))
}
case .Failure(let err):
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
}
答案 0 :(得分:0)
我曾经发送过转换为字符串的图像。这对我有用......
public static void uploadImage(final String userName, final Bitmap bitmap, final Bitmap thumbBitmap, final Activity activity) {
//Showing the progress dialog
String UPLOAD_URL = "your/url/address";
RequestQueue queue = Volley.newRequestQueue(activity);
final String image = getStringImage(bitmap);
final ProgressDialog loading = ProgressDialog.show(activity, "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = "user_name";
//Creating parameters
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
params.put("image", image);
params.put("userName", name);
//returning parameters
return params;
}
};
//Adding request to the queue
queue.add(stringRequest);
}
public static String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
答案 1 :(得分:0)
尝试使用截击的多部分请求。
请参阅以下链接 enter link description here