Created
May 9, 2013 15:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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