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 (); }
}