Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Xutils3网络请求

Android Xutils3网络请求的封装详解及实例代码

投稿:lqh

这篇文章主要介绍了Android Xutils3网络请求的封装详解及实例代码的相关资料,需要的朋友可以参考下

 Xutils3网络请求的封装详解

封装了一个Xutil3的网络请求工具类,分享给大家,本人水平有限,不足之处欢迎指出。

使用前先配置xutils3:

1.gradle中添加

compile 'org.xutils:xutils:3.3.40'

2.自定义Application

/**
 * Created by Joe on 2016/9/25.
 */
public class MyApp extends Application {
  @Override
  public void onCreate() {
    super.onCreate();

    x.Ext.init(this);
    x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
  }
}

3.清单文件application节点点添加

 android:name=".activitys.MyApp"

上面三步走完接下来就可以使用了

先写一个单利类,话不多说上代码

 private volatile static XutilsHttp instance;
  private Handler handler;
  private XutilsHttp(){
    handler = new Handler(Looper.getMainLooper());
  }

  /**
   * 单利模式
   * @return
   */
  public static XutilsHttp getInstance(){
    if (instance == null){
      synchronized (XutilsHttp.class){
        if (instance == null){
          instance = new XutilsHttp();
        }
      }
    }
    return instance;
  }

写好接口

 //接口回调
  public interface XCallBack{
    void onResponse(String result);
    void onFail(String result);
  }
  //下载的接口回调
  public interface XDownLoadCallBack{
    void onstart();
    void onLoading(long total,long current,boolean isDownloading);
    void onSuccess(File result);
    void onFail(String result);
    void onFinished();
  }

封装普通get请求

  /**
   * 普通get请求
   * @param url
   * @param maps
   * @param callback
   */
  public void get(String url, Map<String,String> maps, final XCallBack callback){
    RequestParams params = new RequestParams(url);
    if (null != maps && !maps.isEmpty()){
      for (Map.Entry<String,String> entry : maps.entrySet()){
        params.addQueryStringParameter(entry.getKey(),entry.getValue());
      }
    }
    x.http().get(params, new Callback.CommonCallback<String>() {
      private boolean hasError = false;
      private String result = null;
      @Override
      public void onSuccess(String result) {
        if (result != null) {
          this.result = result;
        }
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
        hasError = true;
        Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
      }

      @Override
      public void onCancelled(CancelledException cex) {

      }

      @Override
      public void onFinished() {
        if (!hasError && result != null) {
          onSuccessResponse(result,callback);
        }
      }
    });
  }

带缓存的get请求

 /**
   * 带缓存数据的异步get请求
   * @param url
   * @param maps
   * @param ifCache   是否缓存
   * @param cacheTime  缓存存活时间
   * @param callback
   */
  public void getCache(String url,Map<String,String> maps,final boolean ifCache,long cacheTime,final XCallBack callback){
    RequestParams params = new RequestParams(url);
    params.setCacheMaxAge(cacheTime);
    if (null != maps && maps.isEmpty()){
      for (Map.Entry<String,String> entry : maps.entrySet()){
        params.addQueryStringParameter(entry.getKey(),entry.getValue());
      }
    }
    x.http().get(params, new Callback.CacheCallback<String>() {
      private boolean hasError = false;
      private String result = null;
      @Override
      public boolean onCache(String result) {
        if (ifCache && null != result){
          this.result = result;
        }
        // true: 信任缓存数据, 不在发起网络请求; false不信任缓存数据.
        return ifCache;
      }

      @Override
      public void onSuccess(String result) {
        if (result != null) {
          this.result = result;
        }
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
        hasError = true;
        Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
      }

      @Override
      public void onCancelled(CancelledException cex) {

      }

      @Override
      public void onFinished() {
        if (!hasError && result != null) {
          onSuccessResponse(result,callback);
        }
      }
    });
  }

普通post请求

 /**
   * 异步post请求
   * @param url
   * @param maps
   * @param callback
   */
  public void post(String url, Map<String,String> maps, final XCallBack callback){
    RequestParams params = new RequestParams(url);
    if (null != maps && !maps.isEmpty()){
      for (Map.Entry<String,String> entry : maps.entrySet()){
        params.addBodyParameter(entry.getKey(),entry.getValue());
      }
    }
    x.http().post(params, new Callback.CommonCallback<String>() {
      private boolean hasError = false;
      private String result = null;
      @Override
      public void onSuccess(String result) {
        if (result != null) {
          this.result = result;
        }
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
        hasError = true;
        onFailResponse(ex.getMessage(),callback);
      }

      @Override
      public void onCancelled(CancelledException cex) {

      }

      @Override
      public void onFinished() {
        if (!hasError && result != null) {
          onSuccessResponse(result,callback);
        }
      }
    });
  }

带缓存的post请求

  /**
   * 带缓存数据的异步post请求
   * @param url
   * @param maps
   * @param ifCache   是否缓存
   * @param cacheTime  缓存存活时间
   * @param callback
   */
  public void postCache(String url,Map<String,String> maps,final boolean ifCache,long cacheTime,final XCallBack callback){
    RequestParams params = new RequestParams(url);
    params.setCacheMaxAge(cacheTime);
    if (null != maps && maps.isEmpty()){
      for (Map.Entry<String,String> entry : maps.entrySet()){
        params.addBodyParameter(entry.getKey(),entry.getValue());
      }
    }
    x.http().get(params, new Callback.CacheCallback<String>() {
      private boolean hasError = false;
      private String result = null;
      @Override
      public boolean onCache(String result) {
        if (ifCache && null != result){
          this.result = result;
        }
        // true: 信任缓存数据, 不在发起网络请求; false不信任缓存数据.
        return ifCache;
      }

      @Override
      public void onSuccess(String result) {
        if (result != null) {
          this.result = result;
        }
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
        hasError = true;
        Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
      }

      @Override
      public void onCancelled(CancelledException cex) {

      }

      @Override
      public void onFinished() {
        if (!hasError && result != null) {
          onSuccessResponse(result,callback);
        }
      }
    });
  }

文件的下载

  /**
   * 下载文件
   * @param url
   * @param filePath
   * @param callback
   */
  public void downFile(String url, String filePath, final XDownLoadCallBack callback){
    RequestParams params = new RequestParams(url);
    params.setSaveFilePath(filePath);
    params.setAutoRename(true);
    x.http().get(params, new Callback.ProgressCallback<File>() {
      @Override
      public void onSuccess(final File result) {
        //下载完成会走该方法
        handler.post(new Runnable() {
          @Override
          public void run() {
            if (callback!=null){
              callback.onSuccess(result);
            }
          }
        });
      }
      @Override
      public void onError(final Throwable ex, boolean isOnCallback) {
        handler.post(new Runnable() {
          @Override
          public void run() {
            if (null != callback){
              callback.onFail(ex.getMessage());
            }
          }
        });
      }
      @Override
      public void onCancelled(CancelledException cex) {
      }
      @Override
      public void onFinished() {
        handler.post(new Runnable() {
          @Override
          public void run() {
            if (callback!=null){
              callback.onFinished();
            }
          }
        });
      }
      //网络请求之前回调
      @Override
      public void onWaiting() {
      }
      //网络请求开始的时候回调
      @Override
      public void onStarted() {
        handler.post(new Runnable() {
          @Override
          public void run() {
            if (null != callback){
              callback.onstart();
            }
          }
        });
      }
      //下载的时候不断回调的方法
      @Override
      public void onLoading(final long total, final long current, final boolean isDownloading) {
        //当前进度和文件总大小
        handler.post(new Runnable() {
          @Override
          public void run() {
            if (callback!=null){
              callback.onLoading(total,current,isDownloading);
            }
          }
        });
      }
    });
  }

文件的上传

  /**
   * 文件上传
   *
   * @param url
   * @param maps
   * @param file
   * @param callback
   */
  public void upLoadFile(String url, Map<String, String> maps, Map<String, File> file, final XCallBack callback) {
    RequestParams params = new RequestParams(url);
    if (maps != null && !maps.isEmpty()) {
      for (Map.Entry<String, String> entry : maps.entrySet()) {
        params.addBodyParameter(entry.getKey(), entry.getValue());
      }
    }
    if (file != null) {
      for (Map.Entry<String, File> entry : file.entrySet()) {
        params.addBodyParameter(entry.getKey(), entry.getValue().getAbsoluteFile());
      }
    }
    // 有上传文件时使用multipart表单, 否则上传原始文件流.
    params.setMultipart(true);
    x.http().post(params, new Callback.CommonCallback<String>() {
      @Override
      public void onSuccess(String result) {
        onSuccessResponse(result, callback);
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {

      }

      @Override
      public void onCancelled(CancelledException cex) {

      }

      @Override
      public void onFinished() {

      }
    });

  }

上传Json字符串到服务器

  /**
   * 上传Json串到服务器
   * @param url
   * @param maps 将需要传的各个参数放在Map集合里面
   */
  public void upLoadJson(String url, Map<String, String> maps, final XCallBack callback){
    JSONObject js_request = new JSONObject();//服务器需要传参的json对象
    try {
      for (Map.Entry<String,String> entry : maps.entrySet()){
        js_request.put(entry.getKey(),entry.getValue());
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }

    RequestParams params = new RequestParams(url);
    params.setAsJsonContent(true);
    params.setBodyContent(js_request.toString());

    x.http().post(params, new Callback.CommonCallback<String>() {//发起传参为json的post请求,
      // Callback.CacheCallback<String>的泛型为后台返回数据的类型,
      // 根据实际需求更改
      private boolean hasError = false;
      private String result = null;

      @Override
      public void onSuccess(String result) {
        if (result != null) {
          this.result = result;
        }
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
        ex.printStackTrace();
      }

      @Override
      public void onCancelled(CancelledException cex) {

      }

      @Override
      public void onFinished() {
        if (!hasError && result != null && callback!=null) {
          onSuccessResponse(result,callback);
        }
      }
    });

  }

使用案例:

下载调用:

   XutilsHttp.getInstance().downFile(url, Utils.getSDPath() + "/" + "ShopBox_Guide_Video/" + Utils.getFileName(url), new XutilsHttp.XDownLoadCallBack() {

      @Override
      public void onstart() {
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
      }

      @Override
      public void onLoading(long total, long current, boolean isDownloading) {
        //当前进度和文件总大小
        mProgressDialog.setMax((int) total);
        mProgressDialog.setProgress((int) current);
      }

      @Override
      public void onSuccess(File result) {
        //下载完成会走该方法
        playMedia(result.getPath());
      }

      @Override
      public void onFail(String result) {
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onFinished() {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
      }
    });

带缓存get请求

   XutilsHttp.getInstance().postCache(url, null, true, 60000, new XutilsHttp.XCallBack() {
      @Override
      public void onResponse(String result) {
        // 成功获取数据
      }

      @Override
      public void onFail(String result) {
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
      }
    });

就是这么简单,不足之处请大家多多指点

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
阅读全文