I used the Apache's Multipart Entity Builder. Then just convert your image into a byte array or input stream.
Multipart image upload in Android - porting an app to Android from iOS which used AFNetworking
-
19-09-2022 - |
Question
Im struggling to find a solution or library with good documentation describing how to submit a multipart image to a server. I had a look at loopJ, which I think is very promising however I am still unsure what the best solution is. It would be great if someone could give me advice/strategy or code example I have used in my iOS, (AFNetworking
) shown below:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"userID": @"1234567890"};
// add parameters first (user id) and then multipart image
[manager POST:MAIN_URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// add multipart image
[formData appendPartWithFileData:imageData name:@"uploadFile" fileName:@"uploadFile" mimeType:@"image/jpg"];
} success:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation,
NSError *error) {
NSLog(@"Error: %@", error);
}];
Where imageData
is the Image that needs to be uploaded.
Solution
OTHER TIPS
http://loopj.com/android-async-http/ seems to be pretty easy to use for android networking including multipart (just like AFNetworking for IOS). The only thing I'm still struggling with, how to update a view (progress bar) while it's uploading. I also need to upload bunch of files synchronously (one at a time) which also seems to be not as easy as I thought (as least I have not found a solution for it yet). If anyone has any hints on using loopj asynchronously -- would be much appreciated.