okhttp設定快取
package com.sn.okhttp_8;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
private TextView mText_tv; private static final int SUCCESS = 993; private static final int FALL = 814; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { //載入網路成功,進行UI的更新 case SUCCESS: String text = (String) msg.obj; mText_tv.setText(text); break; //當載入網路失敗,執行的邏輯程式碼 case FALL: Toast.makeText(MainActivity.this, "不好意思,李紅展太帥了,造成網路異常", Toast.LENGTH_SHORT).show(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //對控制元件進行初始化 initView(); } private void initView() { mText_tv = (TextView) findViewById(R.id.text_tv); } public void okhttp_ok(View viwe) { new Thread() { public void run() { OkHttpClient client = new OkHttpClient.Builder().build(); Request request = new Request.Builder().url(Path).build(); Response response = null; try { response = client.newCall(request).execute(); //重定向 System.out.println("Response 1 response: " + response); System.out.println("Response 1 cache response: " + response.cacheResponse()); System.out.println("Response 1 network response: " + response.networkResponse()); String string = response.body().string(); //通過handler物件,把資料傳入到主執行緒,進行UI更新 Message obtain = Message.obtain(); obtain.obj = string; obtain.what = SUCCESS; handler.sendMessage(obtain); } catch (IOException e) { e.printStackTrace(); } } }.start(); } /** * 通過點選事件,非同步網路請求,拿到String資料 * * @param view */ public void okhttp_text(View view) {
// Toast.makeText(MainActivity.this, “哈哈哈哈”, Toast.LENGTH_SHORT).show();
//建立oKHttpClient的物件
OkHttpClient okHttpClient = new OkHttpClient();
//建立Request.Builder.
Request.Builder builder = new Request.Builder();
//使用 Request.Builder 物件,呼叫Url方法,傳入網路路徑
Request.Builder url = builder.url(Path);
//使用 Request.Builder物件,呼叫build方法構建request物件.
Request request = url.build();
//建立一個Call物件,引數就request物件.
Call call = okHttpClient.newCall(request);
//使用call物件,呼叫enqueue方法,請求加入排程(非同步載入)
call.enqueue(new Callback() {
@Override//當請求失敗時,呼叫此方法
public void onFailure(Call call, IOException e) {
handler.sendEmptyMessage(FALL);
}
@Override//當你們請求成功的時候,呼叫此方法.
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
//通過handler物件,把資料傳入到主執行緒,進行UI更新
Message obtain = Message.obtain();
obtain.obj = string;
obtain.what = SUCCESS;
handler.sendMessage(obtain);
}
});
}
/**
*** B.通過點選事件,非同步網路請求,拿到String資料,並進行本地快取,
* @param view
*/
public void okhttp_cache(View view) {
//B.快取大小
int cacheSize = 10 * 1024 * 1024; // 10 MiB
//B.建立Cache物件,檔案存放私有目錄 引數:1.私有目錄檔案物件 2.設定快取大小
Cache cache = new Cache(getCacheDir(), cacheSize);
//B.建立oKHttpClient的物件,進行本地快取(請求頭,響應頭,內容)
OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(cache).build();
Request.Builder builder = new Request.Builder();
Request.Builder url = builder.url(Path);
Request request = url.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override//當請求失敗時,呼叫此方法
public void onFailure(Call call, IOException e) {
handler.sendEmptyMessage(FALL);
}
@Override//當你們請求成功的時候,呼叫此方法.
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
//通過handler物件,把資料傳入到主執行緒,進行UI更新
Message obtain = Message.obtain();
obtain.obj = string;
obtain.what = SUCCESS;
handler.sendMessage(obtain);
}
});
}
}**