學(xué)習(xí)資料:
打算花段時間,系統(tǒng)性的學(xué)習(xí)下OkHttp
相關(guān)知識温学。計劃先從最簡單的get
請求學(xué)起
1. Get請求
網(wǎng)絡(luò)請求接口來自易源-美女圖片
1.1 GetActivity代碼
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_get"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/get_activity_tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAllCaps="false"
android:textColor="@color/colorAccent"
android:textSize="15sp" />
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
直接將TextView
放在一個NestScrollView
中
OKHttp
使用需要3個對象:
OkHttpClient
Request
Call
Activity代碼
public class GetActivity extends AppCompatActivity implements ResultCallback<String> {
private TextView tv_info;
private Platform mPlatform;//平臺對象仗岖,內(nèi)部最終調(diào)用 , handler.post(runnable)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get);
//得到 Platform 對象
mPlatform = Platform.get();
initView();
}
private void initView() {
tv_info = (TextView) findViewById(R.id.get_activity_tv_show);
request();
}
/**
* 請求網(wǎng)絡(luò)
*/
private void request() {
//創(chuàng)建OkHttpClient 設(shè)置超時時間為10s
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).build();
//創(chuàng)建Request
Request request = new Request.Builder().get().url(Urls.GET_URL).build();
//創(chuàng)建Call
Call call = okHttpClient.newCall(request);
//異步執(zhí)行請求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
sendFailResultCallback(e);//失敗回調(diào)
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//請求結(jié)果
ResponseBody responseBody = null;
try {
//判斷請求是否取消
if (call.isCanceled()) {
sendFailResultCallback(new IOException("Request Canceled"));
return;
}
//獲取請求結(jié)果 ResponseBody
responseBody = response.body();
//獲取字符串
String json = responseBody.string();
Log.e("activity", json);
//成功回調(diào)
sendSuccessResultCallback(json);
} catch (Exception e) {//發(fā)生異常揽祥,失敗回調(diào)
sendFailResultCallback(e);
} finally {//記得關(guān)閉操作
if (null != responseBody) {
responseBody.close();
}
}
}
});
}
/**
* 失敗回調(diào)接口方法
*/
@Override
public void sendFailResultCallback(final Exception e) {
mPlatform.execute(new Runnable() {
@Override
public void run() {
String info = "Fail Message --> " + e.getMessage();
tv_info.setText(info);
}
});
}
/**
* 成功回調(diào)方法
*/
@Override
public void sendSuccessResultCallback(final String result) {
mPlatform.execute(new Runnable() {
@Override
public void run() {
tv_info.setText(JsonFormatUtils.formatJson(result));
}
});
}
}
call.execute()
是同步請求重荠,主線程中無法進行網(wǎng)絡(luò)請求,需要使用異步call.enqueue()
方法
由于網(wǎng)絡(luò)請求在子線程中料按,不能更新UI
,考慮結(jié)合使用Handler
载矿,GetActivity
中闷盔,并沒有直接創(chuàng)建出來一個Handler
來使用,而是利用了一個Platform
對象,在onCreat()
方法中逢勾,mPlatform = Platform.get()
1.2 ResultCallback
不明白為啥一開始就想到先寫個接口。果正。盟迟。寫了就寫了吧
public interface ResultCallback <T>{
//失敗時
void sendFailResultCallback(Exception e);
//成功時
void sendSuccessResultCallback(T t);
}
1.3 Platform
之前看鴻洋大神的Android 一個改善的okHttp封裝庫潦闲,在代碼里面看到的這個類
代碼中有句注釋 Copyright (C) 2013 Square, Inc.
,說明源自Square
公司歉闰,在Retrofit
源碼中也有用到,就學(xué)習(xí)下怎么使用
Platform代碼
public class Platform {
private static final Platform PLATFORM = findPlatform();
public static Platform get() {
return PLATFORM;
}
private static Platform findPlatform() {
try {
Class.forName("android.os.Build");
if (Build.VERSION.SDK_INT != 0) {
return new Android();
}
} catch (ClassNotFoundException ignored) {
ignored.printStackTrace();
}
return new Platform();
}
public Executor defaultCallbackExecutor() {
return Executors.newCachedThreadPool();
}
public void execute(Runnable runnable) {
defaultCallbackExecutor().execute(runnable);
}
static class Android extends Platform {
@Override
public Executor defaultCallbackExecutor() {
return new MainThreadExecutor();
}
static class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable r) {
handler.post(r);
}
}
}
}
使用很簡單
mPlatform.execute(new Runnable() {
//UI線程
do something 。昼弟。。
}
這段代碼感覺可以拿來加深理解 多態(tài)
mPlatform = Platform.get()
变骡,在findPlatform()
方法中芭逝,正常情況,返回的new Android()
對象旬盯,實際是通過new Android()
,得到了Platform
的子類Android
的一個對象
之后的mPlatform.execute()
接剩,其實就是子類Android
直接調(diào)用父類Platform
中的execute()
方法
然而在execute()
方法中,調(diào)用了defaultCallbackExecutor()
Platform
中的defaultCallbackExecutor()
方法:
public Executor defaultCallbackExecutor() {
return Executors.newCachedThreadPool();
}
通過newCachedThreadPool()
迂卢,創(chuàng)建出一個默認的緩存線程池
在子類Android
中桐汤,重寫了defaultCallbackExecutor()
方法
@Override
public Executor defaultCallbackExecutor() {
return new MainThreadExecutor();
}
自定義了一個MainThreadExecutor
,內(nèi)部便是對Handler
的回調(diào)怔毛,這個Handler
是通過new Handler(Looper.getMainLooper())
創(chuàng)建出來的一個UI
主線程的Handler
拣度,也就可以直接用來更新UI
mPlatform.execute(Runnbale)
正常情況下,最終會被Handler
回調(diào)
這段代碼的質(zhì)量明顯比上面GetActivity
的代碼高N條街抗果,Square
公司的,不高才怪日麸。GetActivity
中逮光,我多此一舉的寫了個接口
多學(xué)習(xí),以后我也要敲出這樣高質(zhì)量的代碼涕刚,哈哈 :)
2. 最后
最簡單的異步get
請求,下篇接著學(xué)習(xí)post
請求极景,文件上傳下載之類的
有錯誤請指出
共勉 :)