jackson 解析结合类(需要传入Class, 和 Class.Class, 回调方法是List<Class>)

import java.util.HashMap;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;

import com.leighyu.utils.Constants;

public class AsyncGetProductCategories<T> implements Runnable {

    private final Class<T> type;
    public interface OnFinishListener<T> {
        public void onFinish(List<T> t);
    }
    private OnFinishListener<T> onFinishListener;
    public void setFinishListener(OnFinishListener<T> onFinishListener) {
        this.onFinishListener = onFinishListener;
    }
    
    private String url;
    private HashMap<String, String> params;
    public AsyncGetProductCategories(Class<T> type) {
        this.type = type;
        this.url = Constants.URL + "productcategorylist/getalllevelone.html";
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("testkey", "testvalue");
        this.params = params;
    }
    
    @Override
    public void run() {
        try {
            String result = WebUtil.httpPostJson(this.url, this.params);
            
            ObjectMapper mapper = new ObjectMapper();
            JavaType listType = TypeFactory.collectionType(List.class, type);
            List<T> entities = mapper.readValue(result, listType);
            if(onFinishListener != null) {
                onFinishListener.onFinish(entities);
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
        AsyncGetProductCategories<ProductCategoryEntity> agpc = 
                new AsyncGetProductCategories<ProductCategoryEntity>(ProductCategoryEntity.class);
        agpc.setFinishListener(new OnFinishListener<ProductCategoryEntity>(){
            @Override
            public void onFinish(final List<ProductCategoryEntity> entities) {
                activity.runOnUiThread(new Runnable() 
                {
                    @Override
                    public void run() {
                        drawLevelOne(entities);
                        mProgressDialog.dismiss();
                    }
                });
            }
        });
        
        new Thread(agpc).start();

 

posted @ 2015-10-02 16:23  webglcn  阅读(1114)  评论(0编辑  收藏  举报