安卓學習-其他-http網路工具-用了xutils

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   java   ar   

有個項目當時寫的,不是很完善,基本能用

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網路工具-用了xutils

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.