okhttp
来源:互联网 发布:网络短信平台免费 编辑:程序博客网 时间:2024/06/10 08:20
package com.example.diynsg.net;import android.content.Context;import android.os.Handler;import android.os.Looper;import android.widget.Toast;import com.example.diynsg.bean.BaseBean;import com.example.diynsg.utils.NetWorkUtil;import com.google.gson.Gson;import java.io.IOException;import java.util.HashMap;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 zwj on 2017/10/1. */public class HttpUtil { private static volatile HttpUtil httpUtil; private Context context; private Handler handler = new Handler(Looper.getMainLooper()); private final OkHttpClient client; public HttpUtil(Context context) { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); client = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .build(); this.context = context; } public static HttpUtil getInstance(Context context) { if (httpUtil == null) { synchronized (HttpUtil.class) { if (httpUtil == null) { httpUtil = new HttpUtil(context); } } } return httpUtil; } public void doPost(String url, Map<String, String> params, final Class clazz, final OnNetListener onNetListener) { //网络判断 if (!NetWorkUtil.isNetworkAvailable(context)) { Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_SHORT).show(); return; } if (params != null && params.size() > 0) { FormBody.Builder builder = new FormBody.Builder(); params.put("client", "android"); 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) { } @Override public void onResponse(Call call, Response response) throws IOException { final BaseBean baseBean = (BaseBean) new Gson().fromJson(response.body().string(), clazz); int code = baseBean.getCode(); if (code == 200) { handler.post(new Runnable() { @Override public void run() { try { onNetListener.onSuccess(baseBean); } catch (IOException e) { e.printStackTrace(); } } }); } else if (code == 400) { } else if (code == 300) { } } }); } } public void doGet(String url, final Class clazz, final OnNetListener onNetListener) { //网络判断 if (!NetWorkUtil.isNetworkAvailable(context)) { Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_LONG).show(); return; } Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { onNetListener.onError(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); final BaseBean beseBean = (BaseBean) new Gson().fromJson(string, clazz); int code = beseBean.getCode(); if (code == 200) { handler.post(new Runnable() { @Override public void run() { try { onNetListener.onSuccess(beseBean); } catch (IOException e) { e.printStackTrace(); } } }); } else if (code == 400) { } else if (code == 300) { } } }); } /** * @param url * @param params * @param header * @param clazz * @param onNetListener */ public void doGet(String url, HashMap<String, String> params, HashMap<String, String> header, final Class clazz, final OnNetListener onNetListener) { //网络判断 if (!NetWorkUtil.isNetworkAvailable(context)) { Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_SHORT).show(); return; } Request.Builder builder = new Request.Builder(); StringBuilder sb = new StringBuilder(); if(params!=null){ for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(url); sb.append("?"); sb.append(entry.getKey()); sb.append("="); sb.append(entry.getValue()); } String finalUrl = sb.toString(); builder.url(finalUrl); }else{ builder.url(url); } //添加请求头 if (header!=null) { if (params != null && header.size() > 0) { for (Map.Entry<String, String> entry : params.entrySet()) { builder.addHeader(entry.getKey(), entry.getValue()); } } } final Request request = builder.build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { onNetListener.onError(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); final BaseBean baseBean = (BaseBean) new Gson().fromJson(string, clazz); int code = baseBean.getCode(); if (code == 200) { handler.post(new Runnable() { @Override public void run() { try { onNetListener.onSuccess(baseBean); } catch (IOException e) { e.printStackTrace(); } } }); } else if (code == 400) { } else if (code == 300) { } } }); }}
//OnNetListener类
package com.example.diynsg.net;import com.example.diynsg.bean.BaseBean;import java.io.IOException;/** * Created by zwj on 2017/10/10. */public interface OnNetListener { public void onSuccess(BaseBean baseBean) throws IOException; public void onError(IOException e);}
//网络判断类
package com.example.diynsg.utils;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;/** * Created by zwj on 2017/10/1. * 检查wifi 和 mobile */public class NetWorkUtil { 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 typeName = net.getTypeName(); if (typeName.equalsIgnoreCase("WIFI")){ if (net.isConnected()){ hasWifoCon=true; } } if (typeName.equalsIgnoreCase("MOBILE")){ if (net.isConnected()){ hasMobileCon=true; } } } return hasWifoCon || hasMobileCon; }}
阅读全文
0 0
- OkHttp
- okhttp
- OkHttp
- OkHttp
- OkHttp
- okhttp
- OKHTTP
- okhttp
- Okhttp
- okHttp
- OkHttp
- okhttp
- Okhttp
- okhttp
- OkHttp
- okhttp
- okhttp
- okhttp
- Oracle 12c安装教程
- 习题4(1)
- H5-表格的基本样式
- <<优先级低于+啊
- 模拟实现库函数strlen
- okhttp
- HTML中列表内的各种东西
- 深入理解GRASP
- 读书笔记之JVM垃圾回收
- Anniversary Cake
- HTML中href、src区别
- Oracle wait event --- enq: HW
- 解决Hash碰撞冲突方法总结
- 查看当前使用的shell