封装OkHttp3框架
来源:互联网 发布:淘宝网哪里写招聘 编辑:程序博客网 时间:2024/06/09 21:11
okHttp3是一种非常好用的请求框架,我们可以进行简易封装,然后就可以通调用进行网络请求这里还使用了网络拦截器,用来查看请求与返回的数据,需要依赖的是:compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' package com.bwei.week2moni.net;import android.content.Context;import android.os.Handler;import android.os.Looper;import com.bwei.week2moni.BaseBean.basebean;import com.google.gson.Gson;import java.io.IOException;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;import okhttp3.logging.HttpLoggingInterceptor;/** * Created by 31962 on 2017/10/14. */public class HttpUtil { private static volatile HttpUtil httpUtil; private Context context; private OkHttpClient client; private Handler handler = new Handler(Looper.getMainLooper()); public HttpUtil(Context context) { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); this.context = context; client = new OkHttpClient.Builder() .addInterceptor(logging) .build(); } public static HttpUtil getinstance(Context context){ if(httpUtil==null){ synchronized (HttpUtil.class){ if(httpUtil == null){ httpUtil = new HttpUtil(context); } } } return httpUtil; } public void doGet(String url, final Class clazz, final OnNetListener onNetListener){ if(url!=null){ Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { onNetListener.onError(e); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Gson gson = new Gson(); final basebean b = (basebean) gson.fromJson(string, clazz); String code = b.getCode(); if(code.equals("0")){ handler.post(new Runnable() { @Override public void run() { try { onNetListener.onSuccess(b); } catch (IOException e) { e.printStackTrace(); } } }); } else if(code.equals("1")){ } else{ } } }); } } public void doPost(String url, Map<String,String> params, final Class clazz, final OnNetListener onNetListener){ if(url!=null&¶ms!=null&¶ms.size()>0){ FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String,String > entry:params.entrySet()){ builder.add(entry.getKey(),entry.getValue()); } FormBody formBody = builder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { onNetListener.onError(e); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Gson gson = new Gson(); final basebean b = (basebean) gson.fromJson(string, clazz); String code = b.getCode(); if(code.equals("0")){ handler.post(new Runnable() { @Override public void run() { try { onNetListener.onSuccess(b); } catch (IOException e) { e.printStackTrace(); } } }); } else if(code.equals("1")){ } else{ } } }); } }}这里边的OnNetListener是一个自定义的接口,我们可以进行接口回调public interface OnNetListener { public void onError(IOException e); public void onSuccess(basebean b) throws IOException;}basebean是所有bena类的父类,所以可以通过传入要请求数据的专有的bean.class,实现接口回调,
这个项目还可以加入网络判断,这是网络判断的类:package utils;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;public class NetWorkUtil { // check all network connect, WIFI or mobile public static boolean isNetworkAvailable(final Context context) { boolean hasWifoCon = false; boolean hasMobileCon = false; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfos = cm.getAllNetworkInfo(); for (NetworkInfo net : netInfos) { String type = net.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { if (net.isConnected()) { hasWifoCon = true; } } if (type.equalsIgnoreCase("MOBILE")) { if (net.isConnected()) { hasMobileCon = true; } } } return hasWifoCon || hasMobileCon; }}
这么使用:if(!NetWorkUtil.isNetworkAvailable(context)){ Toast.makeText(context,"没有网络,请查看设置",Toast.LENGTH_SHORT).show(); return;}
阅读全文
1 0
- 封装OkHttp3框架
- 基于OkHttp3封装网络请求框架
- OkHttp3封装
- okhttp3封装
- OkHttp3封装
- 封装OkHttp3
- 优雅设计封装基于Okhttp3的网络框架(一):Http网络协议与Okhttp3解析
- okHttp3自用封装
- okhttp3极简封装
- OkHttp3的简单封装
- OkHttp3 的封装
- OKHTTP3的封装
- Android okhttp3封装
- Rxjava2+okhttp3+Retrofit2封装
- Okhttp3+Rxjava+Retrofit2封装
- Okhttp3封装工具类
- android okhttp3的封装
- Android网络请求框架----Okhttp3完全解析(2),封装框架
- 数据结构实验之链表四:有序链表的归并(STL)
- Scrapy爬虫学习中遇到的问题记录
- 剑指offer之链表中倒数第k个结点(Python)
- Maven profiles构建开发、集测、生产环境配置文件
- 实验二 创建学生成绩-单链表
- 封装OkHttp3框架
- __declspec(dllimport)加和不加的区别
- 算法学习第一章
- noteBook2.3-C#基础第三天
- 欢迎使用CSDN-markdown编辑器
- 03 java基础语法(上)
- NDK实现毛玻璃效果完全教程
- 大数据CDH_10_01
- Java中字符流与字节流的转化