Android: Download a string from a url


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // DOWNLOAD THE PROJECT JSON FILE
  2. HttpClient httpclient = new DefaultHttpClient();
  3. try {
  4. HttpGet httpget = new HttpGet(url);
  5.  
  6. Log.d(TAG, "executing request " + httpget.getURI());
  7.  
  8. // Create a response handler
  9. ResponseHandler<String> responseHandler = new BasicResponseHandler();
  10. String responseBody = httpclient.execute(httpget, responseHandler);
  11.  
  12. //
  13. Log.i(TAG, "DOWNLOADED " + responseBody);
  14.  
  15. } catch (ClientProtocolException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. // When HttpClient instance is no longer needed,
  21. // shut down the connection manager to ensure
  22. // immediate deallocation of all system resources
  23. httpclient.getConnectionManager().shutdown();
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.