How do you Programmatically Download a Webpage in Java

前端 未结 11 2242
无人共我
无人共我 2020-11-22 11:20

I would like to be able to fetch a web page\'s html and save it to a String, so I can do some processing on it. Also, how could I handle various types of compr

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 11:46

    Get help from this class it get code and filter some information.

    public class MainActivity extends AppCompatActivity {
    
        EditText url;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
    
            url = ((EditText)findViewById( R.id.editText));
            DownloadCode obj = new DownloadCode();
    
            try {
                String des=" ";
    
                String tag1= "
    "; String l = obj.execute( "http://www.nu.edu.pk/Campus/Chiniot-Faisalabad/Faculty" ).get(); url.setText( l ); url.setText( " " ); String[] t1 = l.split(tag1); String[] t2 = t1[0].split( "
    " ); url.setText( t2[0] ); } catch (Exception e) { Toast.makeText( this,e.toString(),Toast.LENGTH_SHORT ).show(); } } // input, extrafunctionrunparallel, output class DownloadCode extends AsyncTask { @Override protected String doInBackground(String... WebAddress) // string of webAddress separate by ',' { String htmlcontent = " "; try { URL url = new URL( WebAddress[0] ); HttpURLConnection c = (HttpURLConnection) url.openConnection(); c.connect(); InputStream input = c.getInputStream(); int data; InputStreamReader reader = new InputStreamReader( input ); data = reader.read(); while (data != -1) { char content = (char) data; htmlcontent+=content; data = reader.read(); } } catch (Exception e) { Log.i("Status : ",e.toString()); } return htmlcontent; } } }

提交回复
热议问题