jackson 解析结合类(需要传入Class, 和 Class.Class, 回调方法是List<Class>)
2015-10-02 16:23
519 查看
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();
相关文章推荐
- hdu 4813(2013长春现场赛A题)
- Hibernate懒加载
- Python基础
- 03 crawler
- ocp-33
- [LeetCode][JavaScript]Reverse Linked List II
- 栈——括号匹配
- ocp-32
- HDU 2100 模拟
- leetcode 288: Unique Word Abbreviation
- Add Two Numbers - leetcode002
- 应用服务器性能优化
- ios动画
- GO RPC
- 零基础学python-19.8 生成器表达式:当迭代器遇上列表解析
- ocp-31
- mac Xcode/iOS license
- 【kmp字符串匹配】hdu 1686 Oulipo
- IOS视图切换
- NGUI点击事件的添加