牛骨文教育服务平台(让学习变的简单)

今天太偷懒了,只学了AsyncTask的一点点内容。今晚还要去上课。加油努力吧。

下面这个代码只是用AsyncTask实现一个很简单的事情“从网上下载图片显示到手机上”,但是作为AsyncTask是一个不错的示例。

顺便讲一下,AsyncTask的重要知识点包括他的三个参数(第一个参数:传入doInBackground()方法的参数类型,第二个参数:传入onProgressUpdate()方法的参数类型 , 第三个参数:传入onPostExecute()方法的参数类型,也是doInBackground()方法返回的类型。)

另外还有三四个重要的函数,其中三个会在下面的代码里面出现。

吐槽一下:之前错了两三次全是因为图片资源错误了,泪奔。、

package com.example.myasynctask;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private Button button;
	private ImageView iv;
	private String path = "http://cdn.duitang.com/uploads/item/201310/17/20131017194234_eZfFU.thumb.600_0.png";
	private ProgressDialog dia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.bt);
		iv = (ImageView) this.findViewById(R.id.iv);
		
		dia = new ProgressDialog(this);
		dia.setTitle("提示信息");
		dia.setMessage("正在下载...");

		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new myTask().execute(path);
			}
		});
	}
	
	public class myTask extends AsyncTask<String, Void, Bitmap>
	{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dia.show();
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			// TODO Auto-generated method stub
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(params[0]);
			Bitmap bitmap = null;
			try {
				HttpResponse httpResponse = httpClient.execute(httpGet);
				if (httpResponse.getStatusLine().getStatusCode() == 200 )
				{
					HttpEntity httpEntity = httpResponse.getEntity();
					byte[] data = EntityUtils.toByteArray(httpEntity);
					bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
				}		
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bitmap;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			iv.setImageBitmap(result);
			dia.dismiss();
		}	
	}
}

一个简简单单的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:gravity="center_vertical"
        android:text="下載圖片" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="156dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>