Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.Thor-jelly:OkhttpUtils:最新版本号'
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
OkHttpClient okHttpClient = new OkHttpClient.Builder()
// .addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
共同baseUrl可以设置baseURL,如果要在网络的时候设置不同的baseUrl可以用.baseUrl来设置,也可以直接用Url来设置全部的网络连接地址
OkHttpUtils.getInstance().setBaseUrl(baseUrl);
OkHttpUtils.getInstance().addCommonParams(commonParams);
自定义callback,重写addChangeCommonParameters()方法返回自己需要的动态请求参数
OkHttpUtils.getInstance().addCommonHeaders(commonHeaders);
CookieJarImpl cookieJar = new CookieJarImpl(new MemoryCookieStore());
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
.writeTimeout(10000L, TimeUnit.MILLISECONDS)
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
.hostnameVerifier(new HttpsUtils.UnSafeHostnameVerifier())
.build();
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
.writeTimeout(10000L, TimeUnit.MILLISECONDS)
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
.hostnameVerifier(new HttpsUtils.UnSafeHostnameVerifier())
.build();
public abstract class WddTestCallback extends Callback<User>
{
@Override
public WddTest parseNetworkResponse(Response response, int id) throws IOException
{
String string = response.body().string();
WddTest wddT = new Gson().fromJson(string, WddTest.class);
return wddT;
}
}
OkHttpUtils
.getWebSocket()
.newBuild()
.execute(new WebSocketListener(){
//重写的方法
});
OkHttpUtils
.get()
.url(url)
.id(100)
.build()
.execute(new StringCallback());
Map<String, String> m = new HashMap();
m.put("name","wdd");
m.put("pwd","123456");
OkHttpUtils
.post()
.url(url)
.params(m)//如果没有就不传
.build()
Map<String, String> m = new HashMap();
m.put("name","wdd");
m.put("pwd","123456");
OkHttpUtils
.postString()
.url(url)
.params(m)//如果没有就不传
.build()
List<FileInput> fileList = new ArrayList();
for(i = 0; i < 4; i++){
fileList.add(new FileInput(key, fileName, file));
}
OkHttpUtils
.postFile()
.url(url)
.mediaType()
.files(fileList)
.build()
.execute(new MyStringCallback());
OkHttpUtils
.get()
.url(url)
.build()
.execute(new FileCallBack("保存路径", "文件名"){
//重写的方法
}