1.添加依賴:compile 'com.zhy:okhttputils:2.6.2'api
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); } });