gpt4 book ai didi

java - 如何从 Android 发送图像并在 GAE 上接收它?

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:43 24 4
gpt4 key购买 nike

我正在使用 native 应用程序和云应用程序组件构建一个非常简单的项目。 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))

我在这里做错了什么?

最佳答案

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

它不是Python语言,但我希望它可以帮助你!

关于java - 如何从 Android 发送图像并在 GAE 上接收它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com