【问题标题】:How to send an image from Android and receive it on GAE?如何从 Android 发送图像并在 GAE 上接收?
【发布时间】:2015-02-05 00:08:55
【问题描述】:

我正在使用本机应用程序和云应用程序组件构建一个非常简单的项目。 android 应用程序只需拍摄一张照片,然后通过 POST 将其上传到 URL。然后云应用将显示接收到的照片,这些照片存储在 GAE 数据存储中。

目前,Android 应用程序似乎 可以正确发送照片,并从服务器接收 HTTP OK。但是,当我去查看云应用上的图像时,我没有得到图像,而且它似乎是一个空白条目。

此时我不确定我的 android 应用程序组件是否无法正确发送图像,或者云应用程序组件是否无法接收图像。通过使用发布到上传网址的简单表单,我已经能够成功测试可以上传的图片,效果很好。

我唯一看到的是,android 代码在任何时候都没有设置标题名称“照片”并将其分配给照片的数据。但是,我似乎无法弄清楚如何设置它,而且我不相信这是这里唯一的问题。

我使用此页面来帮助发送文件的 android 代码:How to send a file in Android from mobile to server using http?

这里是重要的android代码:

private class SendPhotoTask extends AsyncTask<File, Void, Void> {
    @Override
    protected Void doInBackground(File... photoFile) {
        if (photoFile != null) {
            String urlToConnect = "http://example.com/upload";
            // Use a static file for testing
            File photoFile2 = new File("/storage/emulated/0/Pictures/PNG_20141206_190223_-1388180531.png");
            try {
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(urlToConnect);

                InputStreamEntity reqEntity = new InputStreamEntity(
                        new FileInputStream(photoFile2), -1);
                reqEntity.setContentType("binary/octet-stream");
                reqEntity.setChunked(true); // Send in multiple parts if needed
                httppost.setEntity(reqEntity);
                HttpResponse response = httpclient.execute(httppost);
                Log.d(DEBUG_TAG, response.getStatusLine().toString());
            } catch (Exception e) {
                Log.d(DEBUG_TAG, "Exception Caught: " + e);
            }
        }
        return null;
    }
}

SendPhotoTask 类是这样执行的:

new SendPhotoTask().execute(photoFile);

这是服务器上接受我的图像的非常简单的代码(使用 webapp2 用 Python 编写):

class Gallery(BaseRequestHandler):

    def post(self):
        gallery_name = self.request.get('gallery_name',
                                      DEFAULT_GALLERY_NAME)
        greeting = Photo(parent=gallery_key(gallery_name))

        greeting.photo = base64.b64encode(str(self.request.get('photo')))
        greeting.put()

        query_params = {'gallery_name': gallery_name}
        self.redirect('/?' + urllib.urlencode(query_params))

我在这里做错了什么?

【问题讨论】:

  • 我会说首先向 photoFile == null 等边缘情况添加一些日志记录,然后记录服务器上的所有内容以查看是否完成了任何请求。此外,在使用“MultipartEntity”(即使只有一个部分?)时,人们似乎会获得更好的结果,例如这个答案:stackoverflow.com/a/1068132/1334771(以及如何实例化 MultipartEntity stackoverflow.com/a/19196621/1334771)

标签: java android python google-app-engine


【解决方案1】:

这是从 Android 应用程序到应用程序引擎 (Servlet Java) 的响应。您应该使用多部分 http 请求:Photo don't uploaded correctly GCS App Engine

它不是在 Python 中,但我希望它可以帮助你!

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 2013-07-01
    • 2015-08-16
    • 1970-01-01
    • 2015-01-08
    • 2021-12-14
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多