|
| 1 | +package com.lenovo.downloadingwebcontent; |
| 2 | + |
| 3 | +import android.os.AsyncTask; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | + |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.URL; |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | + |
| 14 | +public class MainActivity extends AppCompatActivity { |
| 15 | + |
| 16 | + public class Downloadtask extends AsyncTask<String,Void,String>{ |
| 17 | + |
| 18 | + @Override |
| 19 | + protected String doInBackground(String... params) { |
| 20 | + String result = ""; |
| 21 | + URL url; |
| 22 | + HttpURLConnection urlConnection = null; |
| 23 | + |
| 24 | + try |
| 25 | + { |
| 26 | + url=new URL(params[0]); |
| 27 | + urlConnection= (HttpURLConnection) url.openConnection(); |
| 28 | + InputStream in = urlConnection.getInputStream(); |
| 29 | + InputStreamReader reader=new InputStreamReader(in); |
| 30 | + |
| 31 | + int data = reader.read(); |
| 32 | + while(data != -1) |
| 33 | + { |
| 34 | + char current = (char )data; |
| 35 | + result += current; |
| 36 | + data=reader.read(); |
| 37 | + } |
| 38 | + return result; |
| 39 | + }catch (Exception e) |
| 40 | + { |
| 41 | + e.printStackTrace(); |
| 42 | + return "FAILED"; |
| 43 | + } |
| 44 | + |
| 45 | + //Log.i("url:",params[0]); |
| 46 | + |
| 47 | + } |
| 48 | + } |
| 49 | + @Override |
| 50 | + protected void onCreate(Bundle savedInstanceState) { |
| 51 | + super.onCreate(savedInstanceState); |
| 52 | + setContentView(R.layout.activity_main); |
| 53 | + |
| 54 | + Downloadtask task = new Downloadtask(); |
| 55 | + String result= null; |
| 56 | + try { |
| 57 | + result = task.execute("https://www.ecohosting.co.uk/").get(); |
| 58 | + } catch (InterruptedException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } catch (ExecutionException e) { |
| 61 | + e.printStackTrace(); |
| 62 | + } |
| 63 | + |
| 64 | + Log.i("CONTENTS OF URL",result); |
| 65 | + } |
| 66 | +} |
0 commit comments