安卓学习-其他-http网络工具-用了xutils
2014-09-18 08:54
232 查看
有个项目当时写的,不是很完善,基本能用
package org.excsoft.g; import java.lang.reflect.ParameterizedType; import org.excsoftj.m.Msg; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Handler; import android.os.Message; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class MyRequestCallBack<T> extends RequestCallBack<T>{ boolean showFlag=false; boolean cancelFlag=false; public boolean success=false; public Msg returnMsg=new Msg(); private ProgressDialog pd; private Context context; private Handler mHandler; boolean canCanel=false; java.lang.Class cls; int delayedTime=500; String msg11; //private HttpHandler hddd; public MyRequestCallBack(Context context,boolean canCanel,java.lang.Class cls,String msg11){ super.onStart(); this.context=context; this.canCanel=canCanel; this.cls=cls; this.msg11=msg11; } public MyRequestCallBack(Context context,boolean canCanel,java.lang.Class cls,String msg11,int delayedTime){ super.onStart(); this.context=context; this.canCanel=canCanel; this.cls=cls; this.msg11=msg11; this.delayedTime=delayedTime; } public void onStart() { showFlag=true; cancelFlag=false; Message message=new Message(); message.what=1; mHandler=new Handler() { public void handleMessage(Message msg) { if(showFlag){ pd=ProgressDialog.show(context, "", msg11,true,canCanel); pd.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { cancelFlag=true; } }); } super.handleMessage(msg); } }; mHandler.sendMessageDelayed(message,delayedTime); } public void onSuccess(ResponseInfo<T> responseInfo) { try{ showFlag=false; if(pd!=null){ pd.dismiss(); } mHandler.removeMessages(1); if(!cancelFlag){ final XStream xstream = new XStream(new DomDriver()); xstream.ignoreUnknownElements(); xstream.alias("msg", Msg.class); xstream.alias("item", cls); String res=responseInfo.result+""; if(res.indexOf("flag")>0){ returnMsg=(Msg)xstream.fromXML(res); if(returnMsg.getFlag().equals("1")){ success=true; }else{ final String sm=returnMsg.getSm(); new AlertDialog.Builder(context).setTitle("提示").setMessage(sm).setNegativeButton("确定", null).show(); } }else{ new AlertDialog.Builder(context).setTitle("失败") .setMessage("原因:网络不可用").setNegativeButton("确定", null).show(); } } }catch(Exception e){ new AlertDialog.Builder(context).setTitle("失败") .setMessage("原因:网络不可用").setNegativeButton("确定", null).show(); } } // @Override // public void onLoading(long total, long current, boolean isUploading) { // super.onLoading(total, current, isUploading); // if(cancelFlag&&(hddd!=null)){ // hddd.stop(); // } // } @Override public void onFailure(HttpException error, String msg) { if(pd!=null){ pd.dismiss(); } mHandler.removeMessages(1); if(!cancelFlag){ new AlertDialog.Builder(context).setTitle("失败") .setMessage("原因:网络不可用").setNegativeButton("确定", null).show(); } } }
相关文章推荐
- 安卓学习-其他-网络状态工具
- 安卓学习-其他-字符串转码工具
- 安卓网络开发之http协议的概述(学习安卓网络编程语言的学习心得)
- 安卓学习-其他-日期工具
- JSP网络编程-请求和响应-HttpServletRequest-HttpServletResponse-学习笔记
- IOS学习十四:网络之ASIHttpRequest和SBJson简单使用
- 建模工具Rose的学习(转载:http://www.cnblogs.com/s021368/archive/2008/08/15/1268510.html)
- Java网络编程:实现HTTP断点续传下载工具(附源代码)
- 《网络基础学习之三》认识网线制作工具
- Java网络编程之实现HTTP断点续传下载工具(附源代码)
- WIFI直连模块。可以直连手机,电脑,其他模块。集成完整网络协议,内置HTTP DHCP sever
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- iOS学习——iOS网络通信http之NSURLConnection(八)
- wget用法wget是一个从网络上自动下载文件的自由工具。它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.
- 图形、游戏物理 学习、开发、工具(来自网络的很多免费的好资源 )
- 学习内核要准备什么测试工具http://bbs.linuxpk.com/viewthread.php?tid=13066
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- 学习笔记----linux网站网络管理工具
- android 多媒体部分学习十四---网络视频播放(http/rtsp)