Skip to content

Instantly share code, notes, and snippets.

@TomTasche
Created May 9, 2013 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomTasche/5548110 to your computer and use it in GitHub Desktop.
Save TomTasche/5548110 to your computer and use it in GitHub Desktop.
a demonstration of a file upload without any additional libraries on Android. More information here: http://blog.tomtasche.at/2013/05/android-upload-file-to-appengine.html
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
InputStream stream = req.getInputStream();
// do whatever you want with the stream now
} catch (Exception ex) {
throw new ServletException(ex);
}
}
Uri uri = null; // a Uri pointing to the file to be uploaded
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SERVER_URL);
InputStream stream = null;
try {
stream = getContext().getContentResolver().openInputStream(uri);
InputStreamEntity reqEntity = new InputStreamEntity(stream, -1);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
// file uploaded successfully!
} else {
throw new RuntimeException("server couldn't handle request");
}
} catch (Exception e) {
e.printStackTrace();
// handle error
} finally {
stream.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment