从 Android 相机上传到 Python AppEngine (webapp)

发布于 2024-10-27 14:43:08 字数 1935 浏览 3 评论 0原文

我有两个来自 Android 相机服务的 byte[] 数组。我想将它们和一些参数发布到我的 AppEngine 服务器,运行 python webapp 框架。

问题:我不断在服务器端收到空的 HTTP 请求参数。

我的主要方法是 Apache HttpClient:

1) Android 2.x 不包含 MultiPartEntity 类,这是带有二进制文件的多部分所需的。所以我将 httpmime-4.0.1.jar 和 apache-mime4j-0.6.1.jar 添加到构建路径中。

2) Android 端,我正在执行这样的 POST:

public String post(String URI, byte[] jpeg, String description) {
    // Setup MultiPartEntity
    MultipartEntity args = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    args.addPart("mydescription", new StringBody(description));

    // Now add the file
    InputStream s1 = new ByteArrayInputStream(jpeg);
    args.addPart("myfile", new InputStreamBody(s1, "image/jpeg", "1.jpeg"));

    HttpClient httpclient = new DefaultHttpClient();
    // HTTP 1.1 is much faster with HttpClient, same issues w/o it  
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httpost = new HttpPost(URI);
    httpost.setEntity(args);

    HttpResponse response = httpclient.execute(httpost);

    // blah blah blah, process response
}

3) Python AppEngine 端,我的处理程序如下所示:

class UploadHandler(webapp.RequestHandler):
    def post(self, request):
        logging.info(self.request.arguments())
        logging.info(self.request.POST)

4) 参数为空 ->空数组被打印到日志中。来自 webob 的较低级别的 self.request._request__body() 也是空的。不好的迹象!

5)如果我不将InputStreamBody添加到MultipartEntity(仅StringBody参数),一切都会正常,并且mydescription参数会显示出来。

6)我设置了一个PHP服务器并尝试发布: POST 适用于 PHP!

7) HttpClient 发送的格式问题会导致 webapp/webob/wsgi/cgi.FieldStorage 或其他问题。我不知道它哪里坏了。

8)我还尝试根据 RFC 2388 使用 URLConnection 编写原始 http multipart/form,得到类似的结果。 webapp/webob/wsgi/whatever 下面是什么 RFC?

谢谢大家!

这是我的第一个主要 stackoverflow 问题,希望我已经正确格式化了所有内容;-)

I have two byte[] arrays from the Android Camera Service. I want to POST them, and a few parameters to my AppEngine server, running the python webapp framework.

PROBLEM: I keep getting empty HTTP request arguments on the server side.

My main approach has been Apache HttpClient:

1) Android 2.x doesn't include the MultiPartEntity class, needed for multipart w/ binaries. So I've added httpmime-4.0.1.jar and apache-mime4j-0.6.1.jar to the build path.

2) Android side, I'm doing the POST like this:

public String post(String URI, byte[] jpeg, String description) {
    // Setup MultiPartEntity
    MultipartEntity args = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    args.addPart("mydescription", new StringBody(description));

    // Now add the file
    InputStream s1 = new ByteArrayInputStream(jpeg);
    args.addPart("myfile", new InputStreamBody(s1, "image/jpeg", "1.jpeg"));

    HttpClient httpclient = new DefaultHttpClient();
    // HTTP 1.1 is much faster with HttpClient, same issues w/o it  
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httpost = new HttpPost(URI);
    httpost.setEntity(args);

    HttpResponse response = httpclient.execute(httpost);

    // blah blah blah, process response
}

3) Python AppEngine side, my handler looks like:

class UploadHandler(webapp.RequestHandler):
    def post(self, request):
        logging.info(self.request.arguments())
        logging.info(self.request.POST)

4) The arguments are empty -> empty arrays are printed to the log. The lower-level self.request._request__body() from webob is also empty. bad sign!

5) If I don't add the InputStreamBody to the MultipartEntity (only StringBody args) everything works fine, and the mydescription argument shows up.

6) I setup a PHP server and tried posting: the POST works with PHP!

7) Something about the format HttpClient is sending causes webapp/webob/wsgi/cgi.FieldStorage or something problems. I can't figure out where its breaking.

8) I've also tried writing raw http multipart/form according to RFC 2388 using URLConnection, with similar results. What RFC is webapp/webob/wsgi/whatever following?

Thanks all!

This is my first major stackoverflow question, hope I've formatted everything right ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文