Network Access is too important in our daily life. What will our life look like if there is no network? Android phones and browsers are the same. You can also obtain data through network communication, such as webservice and EJB calls. The following uses a small example to obtain an image from the network and display it on the mobile phone. A new ImageView component will be used in development.
1. Write a tool class for processing byte streams
Package org. lxh. util;
Import java. io. ByteArrayOutputStream;
Import java. io. InputStream;
Public class StreamTool {
Public static byte [] readInputStream (InputStream in) throws Exception {
Int len = 0;
Byte buf [] = new byte [1024];
ByteArrayOutputStream out = new ByteArrayOutputStream ();
While (len = in. read (buf ))! =-1 ){
Out. write (buf, 0, len); // write data to the memory
}
Out. close (); // close the Memory output stream
Return out. toByteArray (); // Replace the Memory output stream with a byte array
}
}
2. Write a service class to get the byte array of the image.
Package org. lxh. service;
Import java. io. ByteArrayOutputStream;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java.net. HttpURLConnection;
Import java.net. MalformedURLException;
Import java.net. URL;
Import org. lxh. util. StreamTool;
Import android. util. Log;
Public class WebService {
Public static byte [] getImage (String path ){
URL url;
Byte [] B = null;
Try {
Url = new URL (path); // sets the URL
HttpURLConnection con;
Con = (HttpURLConnection) url. openConnection (); // open the connection
Con. setRequestMethod ("GET"); // sets the Request Method
// Set the connection timeout value to 5 s.
Con. setConnectTimeout (5000 );
InputStream in = con. getInputStream (); // retrieves a byte input stream
B = StreamTool. readInputStream (in );
} Catch (Exception e ){
E. printStackTrace ();
}
Return B; // returns the byte array.
}
}
3. Write a user interface
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/picaddress"
/>
<EditText
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "http://www.bkjia.com/uploadfile/2012/0406/20120406025816157.jpg"
Android: id = "@ + id/imageaddress"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/look"
Android: id = "@ + id/button"
/>
<ImageView
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/image"/>
</LinearLayout>
4. Write an activity class
Package org.lxh.net;
Import org. lxh. service. WebService;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. ImageView;
Import android. widget. Toast;
Public class NetActivity extends Activity {
Private EditText picaddress;
Private Button button;
Private ImageView imageView;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button = (Button) this. findViewById (R. id. button );
ImageView = (ImageView) this. findViewById (R. id. image );
Picaddress = (EditText) this. findViewById (R. id. imageaddress );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
String address = picaddress. getText (). toString ();
Try {
Byte [] data = WebService. getImage (address); // obtain the image input stream
// Generate bitmap based on binary data
Bitmap bit = BitmapFactory. decodeByteArray (data, 0, data. length );
ImageView. setImageBitmap (bit );
} Catch (Exception e ){
Log. e ("NetActivity", e. toString ());
Toast. makeText (NetActivity. this, R. string. error, 1). show ();
}
}
});
}
}
5. Add Network Access Permissions
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "org.lxh.net"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". NetActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<Uses-sdk android: minSdkVersion = "7"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
</Manifest>
6. paste the strings. xml file here.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, NetActivity! </String>
<String name = "app_name"> View images </string>
<String name = "picaddress"> image address </string>
<String name = "look"> View </string>
<String name = "error"> network connection exception </string>
</Resources>
The following code is run and I will upload it to you.
From the treasure Valley