从 Android 相机上传到 Python AppEngine (webapp)
我有两个来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论