網絡的通路在我們日常生活中太重要了,如果沒有網絡我們的生活将會是什麼樣子呢?android手機和浏覽器也是一樣的,也可以通過網絡通訊擷取資料,如調用webservice,EJB等。下面就通過一個小例子從網絡擷取一幅圖檔并顯示在手機上,開發中将會使用到一個新的元件ImageView.
1.寫一個用來處理位元組流的工具類
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); //把資料寫入記憶體
}
out.close(); //關閉記憶體輸出流
return out.toByteArray(); //把記憶體輸出流轉換成byte數組
}
}
2.寫一個得到圖檔byte數組的service類
package org.lxh.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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); //設定URL
HttpURLConnection con;
con = (HttpURLConnection)url.openConnection(); //打開連接配接
con.setRequestMethod("GET"); //設定請求方法
//設定連接配接逾時時間為5s
con.setConnectTimeout(5000);
InputStream in=con.getInputStream(); //取得位元組輸入流
b=StreamTool.readInputStream(in);
} catch (Exception e) {
e.printStackTrace();
return b; //傳回byte數組
3.寫一個使用者操作界面
<?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:text="@string/picaddress"
<EditText
android:layout_width="wrap_content"
android:text="http://www.desk9.com/Desk9Image/21/Desk9_21_1690_35790_S.jpg"
android:id="@+id/imageaddress"
<Button
android:layout_width="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.寫一個activity類
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.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); //得到圖檔的輸入流
//二進制資料生成位圖
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.添加網絡通路的權限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.net"
android:versionCode="1"
android:versionName="1.0">
<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.這裡把strings.xml檔案也貼出來
<resources>
<string name="hello">Hello World, NetActivity!</string>
<string name="app_name">圖檔檢視</string>
<string name="picaddress">圖檔位址</string>
<string name="look">檢視</string>
<string name="error">網絡連接配接異常</string>
</resources>
下面是運作效果圖,代碼我也上傳給大家。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmLaBHMmljM2EjM2gjMzEzXw8CX38CXyAjMxAjMvw1ckF2bsBXdvwFdl5mLuR2cj5Set1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)