1. Are you ready for the Galaxy S20? Here is everything we know so far!

Download web page from android

Discussion in 'Android Development' started by jigglywiggly, May 18, 2010.

  1. jigglywiggly

    jigglywiggly Lurker
    Thread Starter

    so I am looking for the equivelent of you know
    wget or fetch

    I tried this code, except all it does is display , Hello World, Hello Android

    Anyone got any idea how to download a web page?
    Code (Text):
    1. package com.dslice;
    2.  
    3. import java.io.BufferedInputStream;
    4. import java.io.InputStream;
    5. import java.net.URL;
    6. import java.net.URLConnection;
    7.  
    8. import org.apache.http.util.ByteArrayBuffer;
    9.  
    10. import android.app.Activity;
    11. import android.os.Bundle;
    12. import android.os.Handler;
    13. import android.widget.Toast;
    14.  
    15. public class HelloAndroid extends Activity {
    16.     private String html = "";
    17.     private Handler mHandler;
    18.  
    19.     public void onCreate(Bundle savedInstanceState) {
    20.         super.onCreate(savedInstanceState);
    21.         setContentView(R.layout.main);
    22.         mHandler = new Handler();
    23.         checkUpdate.start();
    24.     }
    25.  
    26.     private Thread checkUpdate = new Thread() {
    27.         public void run() {
    28.             try {
    29.                 URL updateURL = new URL("http://iconic.4feets.com/update");
    30.                 URLConnection conn = updateURL.openConnection();
    31.                 InputStream is = conn.getInputStream();
    32.                 BufferedInputStream bis = new BufferedInputStream(is);
    33.                 ByteArrayBuffer baf = new ByteArrayBuffer(50);
    34.                
    35.                 int current = 0;
    36.                 while((current = bis.read()) != -1){
    37.                     baf.append((byte)current);
    38.                 }
    39.  
    40.                 /* Convert the Bytes read to a String. */
    41.                 html = new String(baf.toByteArray());
    42.                 mHandler.post(showUpdate);
    43.                
    44.             } catch (Exception e) {
    45.             }
    46.         }
    47.     };
    48.  
    49.     private Runnable showUpdate = new Runnable(){
    50.         public void run(){
    51.             Toast.makeText(HelloAndroid.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show();
    52.         }
    53.     };
    54. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...