1

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?

Community
  • 1
  • 1
John Zeller
  • 575
  • 2
  • 7
  • 22
  • I'd say start by adding some logging to edgecases like photoFile == null, and log everything that comes on your server to see if any request is done. Also, people seem to get better results when using "MultipartEntity" (even with one part?) like in this answer: http://stackoverflow.com/a/1068132/1334771 (and here on how to instantiate a MultipartEntity http://stackoverflow.com/a/19196621/1334771) – halflings Dec 07 '14 at 09:39

1 Answers1

0

Here is the response from Android App to App engine (Servlet Java). You should use multipart http request: Photo don't uploaded correctly GCS App Engine

Its not in Python but I hope it can help you !

Community
  • 1
  • 1
Phil
  • 4,730
  • 1
  • 41
  • 39