Android中Xutils和Gson的应用Android工程师
1下载Xutils和Gson第三方包
2明确json样式这里给个测试的json样式
{
"pageNum":1,
"tags": [
{
"id": 1,
"product_manager1": "产品经理",
"picSmall": "http://img.mukewang.com/55237dcc0001128c06000338-300-170.jpg",
"picBig": "http://img.mukewang.com/55237dcc0001128c06000338.jpg",
"interview": "27条面试经验",
"time": "03月02日",
"money": "6500-10000/月",
"place": "广州",
"nature": "全职",
"company": "冠鹏公司",
"number": "20-99人"
},
{
"id": 1,
"product_manager1": "产品经理",
"picSmall": "http://img.mukewang.com/55237dcc0001128c06000338-300-170.jpg",
"picBig": "http://img.mukewang.com/55237dcc0001128c06000338.jpg",
"interview": "27条面试经验",
"time": "03月02日",
"money": "6500-10000/月",
"place": "广州",
"nature": "全职",
"company": "冠鹏公司",
"number": "20-99人"
}
]
}
3xutil2.6.14中的HttpUtils类中的send方法中已经做了异步操作
public void httpget(RequestParams requestParams, final Handler mHandler, FragmentActivity fragmentActivity )
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
// LogUtils.d(arg0.result);
Log.d("onSuccess", arg0.result);
mjson = arg0.result;
这里的 arg0.result就是我们通过httpget请求得到的json数据
4将json数据解析
String json=http.getMjson();
@SuppressWarnings("rawtypes")
Map jsonMap = mgson.fromJson(json,Map.class
datas = (List<Object>) jsonMap.get("data");
5将解析到的list<Object>set到与json数据对应的实体类中
httpInfos=new ArrayList<HttpInfo>();
httpInfo = null;
for (int i = 0; i < gsonAnalysis.datas.size(); i++) {
Map<String, Object> obj = (Map<String, Object>) gsonAnalysis.datas
.get(i);
httpInfo.setAddress((String)obj.get("address"));
httpInfo.setName((String)obj.get("name"));
httpInfo.setType((String)obj.get("type"));
6通过实体类去取对应的数据通过get在set到 赋值到对应的android UI控件上
final String maddress="福州市"+httpInfosNum.getAddress();
7图片加载
通过Xutils类中的DefaultBitmapLoadCallBack等方法
public XUtilsImageLoader(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
bitmapUtils = new BitmapUtils(mContext);
bitmapUtils.configDefaultLoadingImage(R.drawable.img1);//默认背景图片
bitmapUtils.configDefaultLoadFailedImage(R.drawable.xutilimageloader_fail);//加载失败图片
bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型
}
/**
*
* @author sunglasses
* @category 图片回调函数
*/
public class CustomBitmapLoadCallBack extends
DefaultBitmapLoadCallBack<ImageView> {
@Override
public void onLoading(ImageView container, String uri,
BitmapDisplayConfig config, long total, long current) {
}
@Override
public void onLoadCompleted(ImageView container, String uri,
Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
// super.onLoadCompleted(container, uri, bitmap, config, from);
fadeInDisplay(container, bitmap);
}
@Override
public void onLoadFailed(ImageView container, String uri,
Drawable drawable) {
// TODO Auto-generated method stub
}
}
private static final ColorDrawable TRANSPARENT_DRAWABLE = new ColorDrawable(
android.R.color.transparent);
/**
* @author sunglasses
* @category 图片加载效果
* @param imageView
* @param bitmap
*/
private void fadeInDisplay(ImageView imageView, Bitmap bitmap) {//目前流行的渐变效果
final TransitionDrawable transitionDrawable = new TransitionDrawable(
new Drawable[] { TRANSPARENT_DRAWABLE,
new BitmapDrawable(imageView.getResources(), bitmap) });
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(500);
}
public void display(ImageView container,String url){//外部接口函数
bitmapUtils.display(container, url,new CustomBitmapLoadCallBack());
}


1914篇文章