Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DownloadFile extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... params) {
- try {
- String html = null;
- html = sendRequest(params[0]);
- Log.d(this.getClass().getName(), html);
- return html;
- } catch (Exception e) {
- Log.e(this.getClass().getName(), "Fehler in der SST", e);
- }
- return null;
- }
- public String sendRequest(String urlStr) throws Exception {
- // Standardvalidierung
- if (urlStr == null || urlStr.isEmpty()) {
- throw new InvalidParameterException("Der Parameter urlStr ist null oder leer!");
- }
- // Verbindung erstellen
- HttpURLConnection connection = null;
- // hier kann eine Exception fliegen, wenn es keine URL ist
- URL url = new URL(urlStr);
- connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.setRequestMethod("GET");
- connection.setUseCaches(false);
- // Antwort erstellen
- StringBuilder str = new StringBuilder();
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String line = "\n";
- while ((line = br.readLine()) != null) {
- str.append(line + System.getProperty("line.separator"));
- }
- // Antwort wieder geben
- return str.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement