Advertisement
Guest User

Download

a guest
Oct 10th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. public class DownloadFile extends AsyncTask<String, Void, String> {
  2.    
  3.  
  4.     @Override
  5.     protected String doInBackground(String... params) {
  6.         try {
  7.  
  8.             String html = null;
  9.  
  10.                 html = sendRequest(params[0]);
  11.                 Log.d(this.getClass().getName(), html);
  12.             return html;
  13.         } catch (Exception e) {
  14.             Log.e(this.getClass().getName(), "Fehler in der SST", e);
  15.         }
  16.         return null;
  17.  
  18.     }
  19.    
  20.     public String sendRequest(String urlStr) throws Exception {
  21.         // Standardvalidierung
  22.         if (urlStr == null || urlStr.isEmpty()) {
  23.             throw new InvalidParameterException("Der Parameter urlStr ist null oder leer!");
  24.         }
  25.  
  26.         // Verbindung erstellen
  27.         HttpURLConnection connection = null;
  28.  
  29.         // hier kann eine Exception fliegen, wenn es keine URL ist
  30.         URL url = new URL(urlStr);
  31.         connection = (HttpURLConnection) url.openConnection();
  32.         connection.setDoInput(true);
  33.         connection.setRequestMethod("GET");
  34.         connection.setUseCaches(false);
  35.  
  36.         // Antwort erstellen
  37.         StringBuilder str = new StringBuilder();
  38.         BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  39.         String line = "\n";
  40.         while ((line = br.readLine()) != null) {
  41.             str.append(line + System.getProperty("line.separator"));
  42.         }
  43.  
  44.         // Antwort wieder geben
  45.         return str.toString();
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement