Simple file uploads from Android to Google App Engine?
I'm writing an An开发者_JAVA技巧droid app that needs to store files on cloud storage accessible from only other Android devices on the same Google account. I figured App Engine would be the best way to go.
I have never used GAE before. I have no idea how to program for it, but I code in Java. Basically, how do I let my Android app upload a file (blob?) to Google App Engine that is tied to their Google account?
Thanks Nikolay, this is exactly what I'm looking for! But there's one problem: This example shows uploading through a web site. I want to know if it's possible to upload through a Java app.
You'll have to do some reading first. Basically, expose a URL from your GAE app that requires authentication. That takes care of only letting people post data if the have a Google account. Then you need to associate each upload with a certain user using the currently logged-in users's email address.
To upload from a Java app you can use the Apache HttpClient (available in Android) to post to the GAE servlet. Just use the same parameters that are in the JSP from.
Here is a list I followed when I did the similar job.
Create an asynchronous task as the body to execute network related task, you can not do network operation on your main UI thread.
You need to get a UPLOAD URL for you to upload the file to, this UPLOAD URL is dynamically generated by the GAE servlet or JSP page calling the BlobStore createUploadURL() method, this method require a url for the next step.
The asynchronous task use URLConnection or HttpClient to upload the file to the url you get in the step 2, at meantime, set the connection to follow the redirect, so the server can send back the blob key.
Receive the blob key responded from the server and use as a ticket to refer the uploaded file at the server side.
Good Luck.
精彩评论