【问题标题】:Using Google BlobStore with an Android application将 Google BlobStore 与 Android 应用程序一起使用
【发布时间】:2011-09-04 20:32:27
【问题描述】:

我找到了 1 个关于这个问题的帖子,它确实部分回答了这个问题,但恐怕我需要一些细节。

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

他是我的密码;

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

我通过一个运行良好的 GET 请求获取 URL。我还尝试了一个包含 ByteArrayBody 的 FormBodyPart,并将 ByteArrayBody 的 mime 类型设置为“multipart/form-data”,但没有任何效果。我总是收到 501 错误(服务器无法处理您的请求)。

谢谢,感谢所有答案。

【问题讨论】:

  • 您是否尝试过将您的 Android 应用上传的数据与常规浏览器表单帖子进行比较,看看差异在哪里?
  • 我使用了wireshark。数据看起来几乎一样,除了边界问题。 “多部分解剖器找不到边界参数”我错过了什么? ://
  • 好吧,这真的很棘手。似乎根本不需要标题,而且它们把一切都搞砸了。

标签: java android google-app-engine http blobstore


【解决方案1】:

好的,经过一番搜索,这里是解决方案;

标题似乎没有像我预期的那样工作,所以我只是删除了它们。

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

【讨论】:

  • 就是这样。整体实体是多部分 mime,您可以在其构造函数中设置每个部分的 mime 类型(可选),然后将其添加到多部分中。
  • 我像你一样尝试过,但它不会将我的图像存储在 blobstore 中。变量 image 是 byte[] 吗??
【解决方案2】:

我使用了以下方法,它有效。

这是一个示例,如果您以 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多