異步任務的工具類

*由於在開發的過程中用異步任務下載的東西一般是json和圖片
*所以本工具類只涉及到這兩種類型,如果有興趣,再或者有其他的需求的話,自己可以擴充工具類的功能

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.security.auth.PrivateCredentialPermission;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

public class PottingAsyncTask extends AsyncTask<String, Void, Object> {

    private OnDownLitener onDownLitener;
    private DownType downType;
    public PottingAsyncTask(DownType downType) {
        // TODO Auto-generated constructor stub
        this.downType = downType;
    }
    //子線程
    @Override
    protected Object doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        byte[] bs = getByteArray(arg0[0]);
        if (bs != null) {
            switch (downType) {
            case JSON:
                String json = new String(bs);
                if(onDownLitener!=null){                    
                    Object datasObject=onDownLitener.downJson(json);
                    return datasObject;
                }
                break;
            case IMAGE:
                Bitmap bitmap = BitmapFactory.decodeByteArray(bs, 0, bs.length);
                return bitmap;
            default:
                break;
            }
        }
        return null;
    }
    //在執行完doInBackground後再執行onPostExecute
    @Override
    protected void onPostExecute(Object result) {
        switch (downType) {
        case JSON:
            if(onDownLitener!=null){
                onDownLitener.paresJson(result);
            }
            break;
        case IMAGE:
            if(onDownLitener!=null){
                onDownLitener.setImage(result);
            }
            break;
        default:
            break;
        }
    }
    //構建接口,使用接口回調的方式控制控件
    public interface OnDownLitener{
        Object downJson(String json);
        void paresJson(Object object);
        void setImage(Object object);
    }
    //獲得byte數組
    private byte[] getByteArray(String urlString) {
        InputStream in = null;
        ByteArrayOutputStream ou = null;
        URL url;
        try {
            url = new URL(urlString);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    .openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(3000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode() == 200) {

                in = httpURLConnection.getInputStream();
                ou=new ByteArrayOutputStream();
                byte[] buffer = new byte[1024 * 8];
                int len = -1;

                while ((len = in.read(buffer)) != -1) {
                    ou.write(buffer, 0, len);
                }
                return ou.toByteArray();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (ou != null) {
                try {
                    ou.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    //藉助泛型,確定所需要的類型
    public static enum DownType {
        JSON, IMAGE
    }
    //接口回調的方法
    public PottingAsyncTask setOnDownLitener(OnDownLitener onDownLitener) {
        this.onDownLitener = onDownLitener;
        return this;//返回this的原因是鏈式編程,返回當前PottingAsyncTask的對象
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章