1.添加依赖:compile 'com.zhy:okhttputils:2.6.2'
2.清单文件中注册初始化:
初始化:OkHttpClient okHttpClient = new OkHttpClient.Builder()
// .addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
3.okhhtp-utils的get请求:
OkHttpUtils.get()
.url("http://apicloud.mob.com/boxoffice/day/query?key=1d120467e646b&area=CN")
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
}
});
4.okhhtp-utils的post请求:
OkHttpUtils.post()
.url("http://apicloud.mob.com/appstore/horoscope/day")
.addParams("date", year1)
.addParams("hour", day1)
.addParams("key", "1d11949556a24")
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
Log.d("sss", "response:::" + response);
}
});
5.okhhtp-utils加载图片:
OkHttpUtils.get()
.url("http://192.168.13.89:8080/Web_server/a.jpg")
.build()
.execute(new BitmapCallback() {
@Override
public void onError(Call call, Exception e, int id) {
}
@Override
public void onResponse(Bitmap response, int id) {
image.setImageBitmap(response);
}
});