package com.bwei.zhoukao_2.data.utils; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; public class OKHttpUtils { private static OKHttpUtils okHttpUtils = new OKHttpUtils(); private OkHttpClient okHttpClient; //1. public static OKHttpUtils getInstance() { //2.保持唯一性 if (okHttpUtils == null ){ synchronized (OKHttpUtils.class){ if (okHttpUtils == null ){ okHttpUtils = new OKHttpUtils(); } } } return okHttpUtils; } private OKHttpUtils() { //4. if (okHttpClient == null ){ synchronized (OKHttpUtils.class){ if (okHttpClient == null ){ okHttpClient = new OkHttpClient.Builder() .build(); } } } } //5.注册post请求 public void loginPost(FormBody formBody , String url, Callback callback) { //7. Request request = new Request.Builder().method("POST", formBody).url(url).build(); //6. okHttpClient.newCall(request).enqueue(callback); } }
OKHttpUtils
最新推荐文章于 2024-09-05 15:52:51 发布