okhttp-utils
??????????????
?okhttp?????okhttp?? https://github.com/square/okhttp.
????okhttp?? 3.3.1
.
??
-
Android Studio
compile 'com.zhy:okhttputils:2.6.2'
-
Eclipse
????jar: okhttputils-2_6_2.jar
????????okhttp?okio?jar????? https://github.com/square/okhttp.
????????????
- ???get??
- ???post??
- ??Http Post???????????
- ????/????
- ?????????
- ????????
- ?????Callback
- ??HEAD?DELETE?PATCH?PUT
- ??session???
- ???????https???????????????
??OkhttpClient
???????????okhttp???????OkhttpClient?????????????Application??? initClient
???????
public class MyApplication extends Application {
@Override
public void onCreate()
{
super.onCreate();
OkHttpClient okHttpClient = new OkHttpClient.Builder() //
.addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
//????
.build();
OkHttpUtils.initClient(okHttpClient);
}
}
????AndroidManifest????
??Cookie(??Session)
??cookie???????cookiejar???????????????
CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
//????
.build();
OkHttpUtils.initClient(okHttpClient);
????????
- PersistentCookieStore //???cookie
- SerializableHttpCookie //???cookie
- MemoryCookieStore //cookie???????
?????????????????????CookieJar?????cookie???????
????????cookie????? https://github.com/franmontiel/PersistentCookieJar.
???????????????????????????????
??Log
???OkhttpClient???????????????????? LoggerInterceptor
????????????Interceptor ?
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggerInterceptor("TAG"))
//????
.build();
OkHttpUtils.initClient(okHttpClient);
??Https
??????????????????? HttpsUtils
- ????????https??
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
//????
.build();
OkHttpUtils.initClient(okHttpClient);
- ???????
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(???inputstream, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager))
//????
.build();
OkHttpUtils.initClient(okHttpClient);
- ????
HttpsUtils.getSslSocketFactory( ???inputstream,
?????inputstream,
???????)
????????????????????????? SSLSocketFactory
???sslSocketFactory???
##??????
GET??
String url = "http://www.csdn.net/"; OkHttpUtils
.get()
.url(url)
.addParams("username", "hyman")
.addParams("password", "123")
.build()
.execute(new StringCallback()
{
@Override
public void onError(Request request, Exception e)
{
}
@Override
public void onResponse(String response)
{
}
}
);
POST??
OkHttpUtils
.post()
.url(url)
.addParams("username", "hyman")
.addParams("password", "123")
.build()
.execute(callback);
Post JSON
OkHttpUtils
.postString()
.url(url)
.content(new Gson().toJson(new User("zhy", "123")))
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.build()
.execute(new MyStringCallback());
????Gson??????????????JSON????????addHeader???contentType???? .mediaType(MediaType.parse("application/json; charset=utf-8"))
.?
Post File
OkHttpUtils .postFile() .url(url) .file(file) .build() .execute(new MyStringCallback());
????????????????
Post????????
OkHttpUtils.post()//
.addFile("mFile", "messenger_01.png", file)//
.addFile("mFile", "test1.txt", file2)//
.url(url)
.params(params)//
.headers(headers)//
.build()//
.execute(new MyStringCallback());
????????? addFile
??????????key??????? <input type="file" name="mFile"/>
?name???
???CallBack
?????? StringCallBack
, FileCallBack
, BitmapCallback
??????????????Callback???????User???
public abstract class UserCallback extends Callback<User> {
@Override
public User parseNetworkResponse(Response response) throws IOException
{
String string = response.body().string();
User user = new Gson().fromJson(string, User.class);
return user;
}
}
OkHttpUtils
.get()//
.url(url)//
.addParams("username", "hyman")//
.addParams("password", "123")//
.build()//
.execute(new UserCallback()
{
@Override
public void onError(Request request, Exception e)
{
mTv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(User response)
{
mTv.setText("onResponse:" + response.username);
}
}
);
?? parseNetworkResponse
???response????????????????????????????????sample?
????
OkHttpUtils// .get()// .url(url)// .build()// .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "gson-2.2.1.jar")// {
@Override
public void inProgress(float progress)
{
mProgressBar.setProgress((int) (100 * progress));
}
@Override
public void onError(Request request, Exception e)
{
Log.e(TAG, "onError :" + e.getMessage());
}
@Override
public void onResponse(File file)
{
Log.e(TAG, "onResponse :" + file.getAbsolutePath());
}
}
);
?????????? FileCallback
?????????????????????
????
OkHttpUtils
.get()//
.url(url)//
.build()//
.execute(new BitmapCallback()
{
@Override
public void onError(Request request, Exception e)
{
mTv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(Bitmap bitmap)
{
mImageView.setImageBitmap(bitmap);
}
}
);
????????? BitmapCallback
???
?????????
new Callback<T>() {
//...
@Override
public void inProgress(float progress)
{
//use progress: 0 ~ 1
}
}
callback???? inProgress
??????????
HEAD?DELETE?PUT?PATCH
OkHttpUtils
.put()//also can use delete() ,head() , patch()
.requestBody(RequestBody.create(null, "may be something"))//
.build()//
.execute(new MyStringCallback());
????requestBody????PUT?PATCH??????????
?????
Response response = OkHttpUtils
.get()//
.url(url)//
.tag(this)//
.build()//
.execute();
execute?????callback??????????Response?
??????
RequestCall call = OkHttpUtils.get().url(url).build();
call.cancel();
??tag????
??????????????????? Object tag
?????? OkHttpUtils.cancelTag(tag)
???
????Activity???Activity???????
OkHttpUtils
.get()//
.url(url)//
.tag(this)//
.build()// @Override protected void onDestroy() {
super.onDestroy();
//???????tag?
OkHttpUtils.cancelTag(this);
//???Activity.this??tag???
}
?????Activity????????Activity????tag????onDestory???????
??
#okhttputils -dontwarn com.zhy.http.** -keep class com.zhy.http.**{
*;
}
#okhttp -dontwarn okhttp3.** -keep class okhttp3.**{
*;
}
#okio -dontwarn okio.** -keep class okio.**{
*;
}