首先,開始最簡單的Android網絡編程實戰,URL實現網絡連接,不懂的童鞋可以參考JAVA中的URL編程,其原理是一樣的,在這裡不再多做解釋。
直接貼出實現Android源代碼:
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(View 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, "獲得圖片失敗", 1).show();
}
}
});
// 獲取網頁源代碼
Button sinaButton = (Button) findViewById(R.id.sinaButton);
sinaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(DataActivity.this,SinaActivity.class);
startActivity(intent);
}
});
}
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11