I am converting someone else's iOS app into android, and I have ran into the issue of not being able to make heads or tails of this multipart entity they are creating.
NSMutableData *body = [NSMutableData data];
// file
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"imagefile\"; filename=\"user_%@.jpg\"\r\n",userID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// UserID
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:userID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
which I think looks something like this
---------------------------14737809831466499882746641449
Content-Disposition: attachment;
name=\"imagefile\"; filename=\"user_" + userID +".jpg\"
Content-Type: application/octet-stream
imagedata
---------------------------14737809831466499882746641449
Content-Disposition: form-data;
name=\"userid\"
userID
---------------------------14737809831466499882746641449--
Now I am trying to rebuild this in java using MultiPartEntity
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
and am stuck on figuring the equivalent multipart for the text.
Any help would be appreciated.
it was this
entity.addPart("imagefile", new ByteArrayBody(imageBytes, "image/jpeg", "user_" + userId + ".jpg"));
entity.addPart("userid", new StringBody(userId));
so much more simple and easy, just had to try 30 different things to get it right.