简体   繁体   English

将 Google BlobStore 与 Android 应用程序一起使用

[英]Using Google BlobStore with an Android application

I found 1 thread about this question, which did partially answer the question, but I'm afraid I may need some details.我找到了 1 个关于这个问题的帖子,它确实部分回答了这个问题,但恐怕我可能需要一些细节。

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).我目前正在尝试将 BlobStore 与我的 android 应用程序一起使用,除了 501 错误(HTTP 服务器无法处理您的请求)之外,我什么也得不到。

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.我通过 GET 请求获得了 URL,该请求运行良好。 I also try a FormBodyPart containing a ByteArrayBody, and also to set mime type for the ByteArrayBody to "multipart/form-data" but nothing worked.我还尝试了一个包含 ByteArrayBody 的 FormBodyPart,并将 ByteArrayBody 的 mime 类型设置为“multipart/form-data”,但没有任何效果。 I always get a 501 error (server can not handle your request).我总是收到 501 错误(服务器无法处理您的请求)。

Thanx, all answers are appreciated.谢谢,感谢所有答案。

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);

I used the following and it worked.我使用了以下,它的工作。

This is an example if you post as gzip content:这是一个示例,如果您发布为 gzip 内容:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM