1

I found 1 thread about this question, which did partially answer the question, but I'm afraid I may need some details.

I'm currently trying to use BlobStore with my android app, and I can't get anything else than a 501 error (the HTTP server can not handle your request).

He is my code ;

HttpPost httpPostImg = new HttpPost(url);
Header header = new BasicHeader("Content-Type", "multipart/form-data");
Header h = new BasicHeader("Connection", "keep-alive");             
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
FormBodyPart form = new FormBodyPart("myFile",new ByteArrayBody(image,"multipart/form- data","pict.jpeg"));
entity.addPart(form);
httpPostImg.setEntity(entity);
httpPostImg.setHeader(header);
httpPostImg.setHeader(h);
response = httpClient.execute(httpPostImg);
processResponse(response);

I get the URL by a GET request which is working quite well. I also try a FormBodyPart containing a ByteArrayBody, and also to set mime type for the ByteArrayBody to "multipart/form-data" but nothing worked. I always get a 501 error (server can not handle your request).

Thanx, all answers are appreciated.

Pierrick Bignet
  • 451
  • 4
  • 11
  • Have you tried comparing the data your Android app is uploading to a regular browser form post, to see where the differences lie? – Nick Johnson May 30 '11 at 22:39
  • I did, using wireshark. The data seems almost the same, except a problem about boundary. "the multi part dissector could not find the boundary parameter" What am I missing ? :/ – Pierrick Bignet May 31 '11 at 09:31
  • Ok, it was really tricky. Seems that headers are not at all required, and that they mess everything up. – Pierrick Bignet May 31 '11 at 16:18

2 Answers2

3

Ok, after some search, here is the solution ;

It seems that headers are not working as I expected, so I just deleted them.

HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("data", new ByteArrayBody(image,"image/jpeg","avatar.jpg"));
httppost.setEntity(entity);
response = httpClient.execute(httppost);
processResponse(response);
Pierrick Bignet
  • 451
  • 4
  • 11
  • That's it. The overall entity is multi-part mime, and you set the mime type of each part (optionally) in it's constructor, then it get's added to the multi-part. – Andrew Mackenzie Jun 02 '11 at 16:41
  • I tried this like you but it doesn't store my image in the blobstore. Is the variable image an byte[]?? – FlavienBert Mar 17 '14 at 22:34
1

I used the following and it worked.

This is an example if you post as gzip content:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;
    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(xml.getBytes());
    } finally {
        if (gzos != null)
            try {
                gzos.close();
            } catch (IOException ex) {
            }
    }

    byte[] fooGzippedBytes = baos.toByteArray();
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("name", new ByteArrayBody(fooGzippedBytes,"application/x-gzip", "filename"));

    httpPost.setEntity(entity);
athspk
  • 6,722
  • 7
  • 37
  • 51
Sridhar
  • 11
  • 1