android应用开发之HttpConnection


HttpConnectionActivity.java


package cn.sanbo.test;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HttpConnectionActivity extends Activity implements OnClickListener {

	private TextView textView;
	private ImageView iv_bitmap;
	private boolean isLoad = false;
	private EditText et;

	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			switch (msg.what) {
			case 1:
				Bitmap bitmap = (Bitmap) msg.obj;
				iv_bitmap.setVisibility(View.VISIBLE);
				iv_bitmap.setImageBitmap(bitmap);
				
				break;
				
			case 2:
				isLoad = false ;
				Toast.makeText(getApplicationContext(), "图片加载失败!", 0).show();
				break;
				
			default:
				break;
			}
		};

	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();

	}

	private void init() {
		this.findViewById(R.id.button).setOnClickListener(this);
		textView = (TextView) this.findViewById(R.id.textView);
		iv_bitmap = (ImageView) this.findViewById(R.id.iv_bitmap);
		et = (EditText) this.findViewById(R.id.et_url);
	}

	public void onClick(View v) {
		if (v.getId() == R.id.button) {
			if (!isLoad) {
				isLoad = true;
				textView.setVisibility(View.VISIBLE);
				new MyThead().start();
				textView.setVisibility(View.GONE);
			} else {
				Toast.makeText(HttpConnectionActivity.this, "图片不需要重复加载", 0)
						.show();
			}
		}
	}

	class MyThead extends Thread {

		public void run() {
			super.run();
			String strUrl = et.getText().toString();
			Message msg = new Message();
			try {
				URL url = new URL(strUrl);
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				int code = connection.getResponseCode();
				if (code == HttpURLConnection.HTTP_OK) {
					InputStream is = connection.getInputStream();
					Bitmap bitmap = BitmapFactory.decodeStream(is);
					msg.what = 1;
					msg.obj = bitmap;
					handler.sendMessage(msg);

				}
			} catch (Exception e) {
				e.printStackTrace();
				msg.what = 2;
				handler.sendMessage(msg);
			}
		}
	}

}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et_url"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="http://g.hiphotos.baidu.com/pic/w%3D230/sign=a703c2f077094b36db921cee93cd7c00/5fdf8db1cb1349549f8dd44b574e9258d1094a09.jpg" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="httpconnention" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="加载图片"
        android:textColor="@android:color/white"
        android:textSize="18dip"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/iv_bitmap"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter"
        android:visibility="gone" />

</LinearLayout>


你可能感兴趣的:(android应用开发之HttpConnection)