在Android上传关于Python的AppEngine和读取文件 [英] Upload from Android on Python AppEngine and read the file

查看:174
本文介绍了在Android上传关于Python的AppEngine和读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Android发送csv文件到Python AppEngine上。我使用的Blob存储区API和要发送的文件,我用 MultipartEntity HttpPost HTTPGET

因此​​,根据Blob存储API,则必须调用方法 create_upload_url('/上传')来生成的网址上传的文件,并使用该网址为在动作的上传文件。正如你可以阅读这里

我在做什么?我把创建这个网址,并回到我的Andr​​oid应用程序的方法。而与此网址我上传的文件。

生成的URL的格式如下:


  

myapp.appspot.com/_ah/upload / A-批次的号码和信件/


基本上是这样的:

的Andr​​oid code

 的HttpClient HttpClient的=新DefaultHttpClient();
 HTTPGET HTTPGET =新HTTPGET(mContext.getString(myapp.appspot.com/get_blobstore_url); HTT presponse urlResponse = httpClient.execute(HTTPGET); 结果= EntityUtils.toString(urlResponse.getEntity()); 乌里了fileURI = Uri.parse(文件:///sdcard/dir/myfile.csv); //获取文件的URI中的SD卡
 档案文件=新的文件(新的URI(fileUri.toString())); //提取从乌里文件 FileBody fileBody =新FileBody(文件,多部分/表单数据); MultipartEntity实体=新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
 entity.addPart(文件,fileBody); HttpPost httpPost =新HttpPost(结果); httpPost.setEntity(实体); HTT presponse响应= httpClient.execute(httpPost);
 response.getStatusLine();

的AppEngine code

 #返回仅在该文件将被上传的URL,所以这个URL可能会在客户端可用于上传文件
类GetBlobstoreUrl(webapp.RequestHandler):
    DEF得到(个体经营):
        logging.info('这里')
        UPLOAD_URL = blobstore.create_upload_url('/上传)
        logging.info(URL BLOB%S,UPLOAD_URL)
        self.response.out.write(UPLOAD_URL)类UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    DEF后(个体经营):
        logging.info('内上传处理程序)
        upload_files = self.get_uploads('文件')
        blob_info = upload_files [0]
        返回blob_info.key()高清的main():
    应用= webapp.WSGIApplication([('/上传,UploadHandler),('/ get_blobstore_url',GetBlobstoreUrl),调试=真)
    run_wsgi_app(应用程序)如果__name__ =='__main__':
    主要()

问题1

当我将文件发送到AppEngine上返回的URL,这将自动调用服务器的方法UploadHandler?因为这种方法里面的日志消息没有被显示,,并在文件被插入,然后用生成的URL在上传文件时,我得到的响应是一个404错误,为什么错误,如果文件正在上传?

问题2

我上传的文件后,我怎么能解析CSV文件中的服务器和文件数据存储在插入所有数据?

感谢。


解决方案

  

当我将文件发送到AppEngine上返回的URL,这将
  自动调用服务器方法UploadHandler?


正确的。


  

当我将文件发送到AppEngine上返回的URL,这将
  自动调用服务器方法UploadHandler?


向我们展示你的服务器日志 - 什么网址你得到的404?你从上传处理程序得到任何日志消息?


  

我上传的文件后,我怎么能解析csv文件服务器
  并插入所有的数据文件到数据存储的?


使用 BlobReader API ,并通过打开文件蟒蛇 CSV 模块。

I want to send a csv file from Android to Python AppEngine. I'm using the Blobstore API and to send the file, I use MultipartEntity, HttpPost and HttpGet.

So, according to the Blobstore API, you must call the method create_upload_url('/upload') to generate the url to upload the file and you use this url as the action to upload the file. As you can read here

What am I doing? I call a method that creates this url and return it to my android app. And with this url I upload the file.

The url generated is in this format:

myapp.appspot.com/_ah/upload/a-lot-of-numbers-and-letters/

Basically like this:

Android Code

 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 Code

# 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()

Question 1

When I send the file to the url the AppEngine returned, this will automatically call the server method UploadHandler? Because the log message inside this method is not being showed, and the file is being inserted, and the response I get when uploading the file with the generated url is a 404 error, why that error if the file is being uploaded?

Question 2

After I upload the file, how can I parse the csv file in the server and insert all the data in the file to the datastore?

Thanks.

解决方案

When I send the file to the url the AppEngine returned, this will automatically call the server method UploadHandler?

Correct.

When I send the file to the url the AppEngine returned, this will automatically call the server method UploadHandler?

Show us your server logs - what URL are you getting the 404 for? Do you get any log messages from the upload handler?

After I upload the file, how can I parse the csv file in the server and insert all the data in the file to the datastore?

Use the BlobReader API, and pass the opened file to the python csv module.

这篇关于在Android上传关于Python的AppEngine和读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆