Android network programming: URL get data and pictures

Source: Internet
Author: User
Tags tostring

First of all, start the simplest network programming combat, URL to achieve network connectivity, do not understand the children's shoes can refer to the Java URL programming, the principle is the same, here no longer to do more explanation.

Directly post the implementation of the source code:

 public class Dataactivity extends activity {private EditText imagepathtext;    
    private static final String tag= "dataactivity";    
       
    Private ImageView ImageView;    
        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    
        Setcontentview (R.layout.main);    
        Imagepathtext = (edittext) Findviewbyid (R.id.imagepath);     
                
               
        imageview= (ImageView) Findviewbyid (R.id.imageview);    
        Button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (V    
                Iew v) {String path = Imagepathtext.gettext (). toString ();    
                    try {byte[] data=nettool.getimage (path); Bitmap bm=bitmapfactory.decodebytearray (data, 0, Data.length);    
                Imageview.setimagebitmap (BM);    
                    catch (Exception e) {log.i (TAG, e.tostring ());    
                Toast.maketext (Dataactivity.this, "Get Picture Failed", 1). Show ();    
                
}    
            }    
        });    
        Get the Web page source Button Sinabutton = (button) Findviewbyid (R.id.sinabutton); Sinabutton.setonclicklistener (New View.onclicklistener () {@Override public void oncli    
                CK (View v) {Intent intent=new Intent (dataactivity.this,sinaactivity.class);    
            StartActivity (Intent);    
       
    }    
        }); }    
}
 public class Sinaactivity extends activity {private TextView TextView;    
private static final String tag= "sinaactivity";    
        Get Web page source code public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    
                
        Setcontentview (R.layout.sina);    
        textview= (TextView) This.findviewbyid (R.id.sina);    
            try {String html=nettool.gethtml ("http://www.sina.com.cn", "GBK");    
        Textview.settext (HTML);    
            catch (Exception e) {log.i (TAG, e.tostring ());    
        Toast.maketext (Sinaactivity.this, "Get Web page Failed", 1). Show (); }    
    }    
}
 public class Nettool {/** * gets URL code data */public static String gethtml (St    
        Ring path,string Encoding) throws Exception {URL url = new URL (path);    
        HttpURLConnection conn = (httpurlconnection) url.openconnection ();    
        Conn.setrequestmethod ("get");    
        Conn.setconnecttimeout (6 * 1000);    
        Not more than 6 seconds.    
        System.out.println (Conn.getresponsecode ());    
            if (Conn.getresponsecode () ==200) {InputStream inputstream=conn.getinputstream ();    
            Byte[] Data=readstream (InputStream);    
        return new String (data,encoding);    
    return null;   
 /** * Gets the data for the specified path.    
        * **/public static byte[] GetImage (String urlpath) throws Exception {URL url = new URL (urlpath);    
        HttpURLConnection conn = (httpurlconnection) url.openconnection ();    
Conn.setrequestmethod ("get");        Conn.setconnecttimeout (6 * 1000);    
        Not more than 6 seconds.    
            if (Conn.getresponsecode () ==200) {InputStream inputstream=conn.getinputstream ();    
        Return Readstream (InputStream);    
    return null; /** * Read Data * input stream * * */public static byte[] Readstream (input    
        Stream instream) throws Exception {Bytearrayoutputstream outstream=new bytearrayoutputstream ();    
        Byte[] Buffer=new byte[1024];    
        int len=-1;    
        while (len=instream.read (buffer)!=-1) {outstream.write (buffer, 0, Len);    
        } outstream.close ();    
                
    Instream.close ();    
return Outstream.tobytearray (); }    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.