I am building a very simple project with a native app and cloud app component. The android app simply takes a photo, and uploads it to a URL via POST. Then the cloud app will display the received photos, that are stored in GAE datastore.
Currently, the android app seems to send a photo correctly, and it receives HTTP OK from the server. But, when I go to check out the image on the cloud app, I am not getting an image, and it seems like a blank entry.
I am unsure at this point if my android app component is failing to send an image correctly, or whether the cloud app component is failing to receive it. I have been able to successfully test that images can upload, by using a simple form that posts to the upload url, and this works great.
The only thing I see off hand is that at no point does the android code set a header name of 'photo' and assign it to the data for the photo. But, I cannot seem to figure out how to set that, and I am not convinced that is the only problem here.
I used this page to help with the android code for sending a file: How to send a file in Android from mobile to server using http?
Here is the important android code:
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;
}
}
The SendPhotoTask class is executed with this:
new SendPhotoTask().execute(photoFile);
And this is the very simple code on the server (written in Python using webapp2) that accepts my images:
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))
What am I doing wrong here?