欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android OKHTTP的单例和再封装的实例

时间:2021-07-15 09:30:45|栏目:Android代码|点击:

Android OKHTTP的单例和再封装的实例

/**
 * Created by zm on 16-2-1
 * okhttp的再封装,对于2.x版本,3.x版本将原有对okhttpclient配置
 * 改成了builder模式配
 * 置,对于超时、代理、dns,okhttp已经做好了配置,
 * 若不需要特殊配置,可以跳过
 */
public class OkHttpUtil
{
  private static OkHttpClient singleton;
  //非常有必要,要不此类还是可以被new,但是无法避免反射,好恶心
  private OkHttpUtil(){

  }
  public static OkHttpClient getInstance() {
    if (singleton == null)
    {
      synchronized (OkHttpUtil.class)
      {
        if (singleton == null)
        {
          singleton = new OkHttpClient();
        }
      }
    }
    return singleton;
  }
}

之前在看okhttp源码的时候,发现square没有对okhttpclient进行单例,网上也没找到合适的解释,以下是自己的猜测

优点:使用单例模式,避免了多次创建所产生的垃圾

缺点:对于一些特殊需求的代码进行一些灵活的配置,单例模式难以实现

总结:做为优秀的开源框架,square出发点是让用户更好更灵活的使用和扩展,从用户角度来说,对于不需要多次配置的项目,可以手动写一个单例模式,便于内存的高效利用

/**
 * okhttp再次封装
 * Created by zm on 16-2-1
 * update by zm on 16-3-19 增加Builder,方便以后内容或者字段的扩展
 * 
 */
public class HttpTools
{
  private Context context;
  private final RequestParams req;
  private final Handler handler;

  public HttpTools(Builder builder)
  {
    // TODO Auto-generated constructor stub
    context = builder.context;
    req = builder.req;
    handler = builder.handler;
  }

  public static class Builder
  {
    private final RequestParams req;
    private final Context context;
    private final Handler handler;

    public Builder(RequestParams req, Context mContext, Handler handler)
    {
      // TODO Auto-generated constructor stub
      this.req = req;
      this.context = mContext;
      this.handler = handler;
    }

    public HttpTools build() {
      return new HttpTools(this);
    }
  }

  public void requestBuilder() {
    // TODO Auto-generated method stub
    if(req==null||context==null||handler==null){
      throw new NullPointerException("NullPointerException");
    }
    requestGet(req, context, handler);
  }

  private static void parse(Call call, final Handler handler,
      final RequestParams req) {
    // 请求加入调度
    call.enqueue(new Callback()
    {
      @Override
      public void onResponse(Call call, Response response)
          throws IOException {
        // TODO Auto-generated method stub
        String result = response.body().string();
        if (result != null)
        {
          Message message = Message.obtain();
          message.obj = result;
          message.what = req.getSuccessMsgWhat();
          handler.sendMessage(message);
        }
      }

      @Override
      public void onFailure(Call call, IOException e) {
        // TODO Auto-generated method stub
        handler.sendEmptyMessage(req.getFailMsgWhat());
      }
    });
  }

  /**
   * 
   * @param req
   * @param context
   * @param handler
   * 
   *      get请求
   */
  public static void requestGet(final RequestParams req,
      final Context context, final Handler handler) {
    // 创建一个Request
    final Request request = new Request.Builder().url(req.getRequestUrl()).build();
    Call call = OkHttpUtil.getInstance().newCall(request);
    parse(call, handler, req);
  }

  /**
   * post请求
   */
  public static void requestPost(final RequestParams req,
      final Context context, final Handler handler) {
    FormBody.Builder builder = new FormBody.Builder();
    //此处是对RequestParams的遍历,RequestParams类省略
    for (Map.Entry<String, Object> mEntry : req.getParamEntry())
    {
      String mEntryKey = mEntry.getKey();
      Object mEntryValue = mEntry.getValue();
      if (TextUtils.isEmpty(mEntryKey))
      {
        continue;
      }
      builder.add(mEntryKey, mEntryValue.toString());
    }
    RequestBody body = builder.build();
    Request request = new Request.Builder().url(req.getUrl()).post(body).build();

    Call call = OkHttpUtil.getInstance().newCall(request);
    parse(call, handler, req);
  }

  /**
   *      数据请求的集中管理,方便以后一键替换,从get到post
   */
  public static void request(RequestParams req, Context mContext,
      Handler handler) {
    // TODO Auto-generated method stub
    requestGet(req, mContext, handler);
  }
}

最后再奉献上一个封装类

/**
 * 
 * Created by zm on 16-2-1
 * 基于Gson的json转model封装类
 *
 */
public class JsonToModel
{

private static String info = "info";

  public static String getInfo()
  {
    return info;
  }

  public static void setInfo(String info)
  {
    JsonToModel.info = info;
  }
  /**
   * 
   * @param msg
   * @param t
   *      model类
   * @param model
   *      model对象
   * @return
   */
  public static <T> List<T> getJsonArrayToModel(Message msg, Class<T> t,
      T model) {
    // TODO Auto-generated method stub
    List<T> list = new ArrayList<T>();
    try {
      JSONObject json = new JSONObject(msg.obj.toString());
      for (int i = 0; i < json.getJSONArray(getInfo()).length(); i++) {
        model = GsonHelper.toType(json.getJSONArray(getInfo()).get(i).toString(), t);
        list.add(model);
      }
      return list;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      Log.e("getJsonArrayToModel", "error");
      e.printStackTrace();
    }
    return null;
  }
}

json转model的这个类中,当时没考虑到过多性能的问题,在此类中即使用了org.json.JSONObject也使用了gson,此处还可以做出相应的优化

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:android 横竖屏限制的配置方法

栏    目:Android代码

下一篇:Android布局案例之人人android九宫格

本文标题:Android OKHTTP的单例和再封装的实例

本文地址:http://www.codeinn.net/misctech/157524.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有