Android中怎么利用OKHTTP实现单例
短信预约 -IT技能 免费直播动态提醒
本篇文章为大家展示了Android中怎么利用OKHTTP实现单例,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Android 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出发点是让用户更好更灵活的使用和扩展,从用户角度来说,对于不需要多次配置的项目,可以手动写一个单例模式,便于内存的高效利用
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()); } }); } 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); } 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); } public static void request(RequestParams req, Context mContext, Handler handler) { // TODO Auto-generated method stub requestGet(req, mContext, handler); }}
最后再奉献上一个封装类
public class JsonToModel{private static String info = "info"; public static String getInfo() { return info; } public static void setInfo(String info) { JsonToModel.info = info; } 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; }}
上述内容就是Android中怎么利用OKHTTP实现单例,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341