zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    mysql5.7.17安装配置图文教程
    Idea破解
    git pull总是要输入账号和密码
    java 查看线程死锁
    SpringBoot:四种读取properties文件的方式
    Springboot的常规属性配置和类型安全配置
    java web应用连接mysql会突然connection连接失败
    设计模式--单例模式(二)登记式
    设计模式--单例模式(一)懒汉式和饿汉式
    Springboot的2种启动方式
  • 原文地址:https://www.cnblogs.com/webglcn/p/4852363.html
Copyright © 2011-2022 走看看