Android xutil3网络模块二次封装

在Android开发过程中,难免有网络请求,通常的在子线程中请求网络,在主线程中更新UI,这样的话比较麻烦,国内大神写了好多框架,其中最常见的就是xutil了,在开发过程中,需要实现它的网络请求部分的回调函数的好几个方法,这样显得代码有点乱,于是乎,我就想对他进行封装了。其实我是按照开发网站的思想进行封装的,做个网站的都知道,MVC思想很重要,特别是在ssh或ssm框架中较为明显,所以我就按照这个思路进行封装了,下面来看看代码吧

BaseDao.java

package com.example.dao.base;

import java.util.Map;

import org.xutils.x;
import org.xutils.common.Callback.Cancelable;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.RequestParams;

import android.app.Application;
import android.content.Context;

/** * 二次封装xutil3 * @author double江 * */
public class BaseDao{
    private Context context;
    public  void BasDao(Context context){
        this.context=context;
        x.Ext.init((Application) context);
    }
    /** * 发送get请求 * @param <T> */
    public  <T> Cancelable Get(String url,Map<String,String> map,CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        Cancelable cancelable = x.http().get(params, callback);

        return cancelable;
    }

    /** * 发送post请求 * @param <T> */
    public  <T> Cancelable Post(String url,Map<String,Object> map,CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }


    /** * 上传文件 * @param <T> */
    public  <T> Cancelable UpLoadFile(String url,Map<String,Object> map,CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    /** * 下载文件 * @param <T> */
    public  <T> Cancelable DownLoadFile(String url,String filepath,CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        //设置断点续传
        params.setAutoResume(true);
        params.setSaveFilePath(filepath);
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }
}

在activity中
测试一个方法

“`
private void getxml() {
String url=”http://flash.weather.com.cn/wmaps/xml/china.xml”;

    BasDao.Get(url, null, new CallbackDao<String>(){
        @Override
        public void onSuccess(String xmlString) {
            super.onSuccess(xmlString);
            try{
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();  
                XmlPullParser xmlPullParser = factory.newPullParser();  
                xmlPullParser.setInput(new StringReader(xmlString));  
                int eventType = xmlPullParser.getEventType();  
                while (eventType != XmlPullParser.END_DOCUMENT) {  
                    switch (eventType) {  
                    case XmlPullParser.START_TAG:  
                        String nodeName = xmlPullParser.getName();  
                        if ("city".equals(nodeName)) {  
                            String pName = xmlPullParser.getAttributeValue(0);  
                            Log.e("TAG", "city is " + pName);  
                        }  
                        break;  
                    }  
                    eventType = xmlPullParser.next();  
                }  
            }catch(Exception e){
                e.printStackTrace();
            }       
        }
        });
    }

“`Android xutil3网络模块二次封装_第1张图片

你可能感兴趣的:(android)