【发布时间】:2011-12-11 21:44:22
【问题描述】:
我想将 csv 文件从 Android 发送到 Python AppEngine。我正在使用 Blobstore API 并发送文件,我使用 MultipartEntity、HttpPost 和 HttpGet。
因此,根据 Blobstore API,您必须调用方法 create_upload_url('/upload') 来生成上传文件的 url,并将此 url 作为 action 上传文件。如您所见here
我在做什么?我调用了一个方法来创建这个 url 并将其返回到我的 android 应用程序。并使用此网址上传文件。
生成的url格式如下:
myapp.appspot.com/_ah/upload/a-lot-of-numbers-and-letters/
基本上是这样的:
Android 代码
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(mContext.getString("myapp.appspot.com/get_blobstore_url");
HttpResponse urlResponse = httpClient.execute(httpGet);
result = EntityUtils.toString(urlResponse.getEntity());
Uri fileUri = Uri.parse("file:///sdcard/dir/myfile.csv"); // Gets the Uri of the file in the sdcard
File file = new File(new URI(fileUri.toString())); // Extracts the file from the Uri
FileBody fileBody = new FileBody(file, "multipart/form-data");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", fileBody);
HttpPost httpPost = new HttpPost(result);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
response.getStatusLine();
AppEngine 代码
# Returns only the URL in which the file will be uploaded, so this URL may be used in client for upload the file
class GetBlobstoreUrl(webapp.RequestHandler):
def get(self):
logging.info('here')
upload_url = blobstore.create_upload_url('/upload')
logging.info("url blob %s", upload_url)
self.response.out.write(upload_url)
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
logging.info('inside upload handler')
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
return blob_info.key()
def main():
application = webapp.WSGIApplication([('/upload', UploadHandler), ('/get_blobstore_url', GetBlobstoreUrl)], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()
问题 1
当我将文件发送到AppEngine返回的url时,这会自动调用服务器方法UploadHandler吗?因为没有显示此方法中的日志消息,并且正在插入文件,并且我在使用生成的url上传文件时得到的响应是404错误,为什么如果文件出现错误正在上传吗?
问题 2
我上传文件后,如何解析服务器中的csv文件并将文件中的所有数据插入数据存储区?
谢谢。
【问题讨论】:
-
android 端使用
/generate_url,而GAE 端使用/get_blobstore_url。这是代码中的错字,还是只是帖子? -
只是帖子。它们是相同的网址。我打错了。修好了。
标签: android python google-app-engine