虽然okttp自动的缓存一般,考虑到信息安全问题,如果信息比较敏感的话,最好自己写在数据库,自己做缓存,
我在tigerokttp上基础修改一些东西,实现我自己的缓存,大家最好别做伸手党,多看看别人的源码,tigerokhttp的设计不一定适合每个人的业务需求,我结合mvp和mvvm做了一个demo
package com.moziqi.testmvvm; /** * Created by moziqi on 16-5-1. */ public interface IMainView { public void doSuccess(String result); public void doFailure(String error); }
package com.moziqi.testmvvm; /** * Created by moziqi on 16-5-1. */ public interface IPresenter extends Runnable { public void cancel(); }
package com.moziqi.testmvvm; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import com.moziqi.testmvvm.databinding.ActivityMainBinding; import cn.ittiger.net.http.TigerOkHttp; public class MainActivity extends FragmentActivity implements IMainView { private MainPresenter mainPresenter = new MainPresenter(this); private ActivityMainBinding activityMainBinding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); mainPresenter.testGet(); } @Override public void doSuccess(String result) { activityMainBinding.setUser(new UserBean(1, result)); } @Override public void doFailure(String error) { activityMainBinding.setUser(new UserBean(1, error)); } @Override protected void onDestroy() { mainPresenter.cancel(); super.onDestroy(); } }
package com.moziqi.testmvvm; import cn.ittiger.net.common.RequestCallback; import cn.ittiger.net.http.TigerOkHttp; import cn.ittiger.net.http.exeception.TigerHttpException; import cn.ittiger.net.http.request.TigerJsonRequest; /** * MVP的深入learn 其他人的框架https://github.com/moziqi/Beam * Created by moziqi on 16-5-1. */ public class MainPresenter implements IPresenter { private IMainView iMainView; private TigerJsonRequest<String> request; private Thread netThread; public MainPresenter(IMainView iMainView) { this.iMainView = iMainView; netThread = new Thread(this); } public void testGet() { request = new TigerJsonRequest<>("https://www.baidu.com"); //request.addHeaders(MApplication.getInstance().getDefaultHeaders()); request.setRequestCallback(new RequestCallback<String>() { @Override public void onSuccess(String result) { iMainView.doSuccess(result); } @Override public void onFailure(TigerHttpException e) { iMainView.doFailure(e.getMessage()); } }); netThread.start(); } @Override public void run() { //http://blog.csdn.net/aminfo/article/details/7903112 TigerOkHttp.getAsync(request); } @Override public void cancel() { TigerOkHttp.cancel(request.getTag()); request = null; netThread = null; } }
package com.moziqi.testmvvm; import android.app.Application; import android.content.Context; import android.telephony.TelephonyManager; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.UUID; import cn.ittiger.net.http.TigerOkHttp; import cn.ittiger.net.http.config.TigerHttpConfig; /** * Created by moziqi on 16-4-30. */ public class MApplication extends Application { private static MApplication mApplication; @Override public void onCreate() { super.onCreate(); mApplication = this; //初始化 TigerOkHttp.init(new TigerHttpConfig(getApplicationContext())); } public static MApplication getInstance() { return mApplication; } /** * cache-control可以在请求的过程中设置缓存策略,会自动添加对于的header头 * * @return */ public Map<String, String> getDefaultHeaders() { SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); Map<String, String> headers = new HashMap<>(); headers.put("User-Agent", getPhoneInfo()); headers.put("Connection", "close"); headers.put("Date", sFormat.format(calendar.getTime())); return null; } /** * 获取文件上传请求头 * * @return */ public Map<String, String> getUploadFileHeaders() { SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); Map<String, String> headers = new HashMap<>(); headers.put("User-Agent", getPhoneInfo()); headers.put("Connection", "keep-alive"); headers.put("Content-Type", "multipart/form-data;boundary=" + UUID.randomUUID().toString()); headers.put("Date", sFormat.format(calendar.getTime())); return null; } /** * 根据自身开发需求,公司需求再加修改 * * @return */ public String getPhoneInfo() { StringBuilder phoneInfo = new StringBuilder(); phoneInfo.append("Product: " + android.os.Build.PRODUCT + System.getProperty("line.separator")); phoneInfo.append("CPU_ABI: " + android.os.Build.CPU_ABI + System.getProperty("line.separator")); phoneInfo.append("TAGS: " + android.os.Build.TAGS + System.getProperty("line.separator")); phoneInfo.append("VERSION_CODES.BASE: " + android.os.Build.VERSION_CODES.BASE + System.getProperty("line.separator")); phoneInfo.append("MODEL: " + android.os.Build.MODEL + System.getProperty("line.separator")); phoneInfo.append("SDK: " + android.os.Build.VERSION.SDK + System.getProperty("line.separator")); phoneInfo.append("VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE + System.getProperty("line.separator")); phoneInfo.append("DEVICE: " + android.os.Build.DEVICE + System.getProperty("line.separator")); phoneInfo.append("DISPLAY: " + android.os.Build.DISPLAY + System.getProperty("line.separator")); phoneInfo.append("BRAND: " + android.os.Build.BRAND + System.getProperty("line.separator")); phoneInfo.append("BOARD: " + android.os.Build.BOARD + System.getProperty("line.separator")); phoneInfo.append("FINGERPRINT: " + android.os.Build.FINGERPRINT + System.getProperty("line.separator")); phoneInfo.append("ID: " + android.os.Build.ID + System.getProperty("line.separator")); phoneInfo.append("MANUFACTURER: " + android.os.Build.MANUFACTURER + System.getProperty("line.separator")); phoneInfo.append("USER: " + android.os.Build.USER + System.getProperty("line.separator")); TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); phoneInfo.append("DeviceId(IMEI) = " + tm.getDeviceId() + System.getProperty("line.separator")); phoneInfo.append("DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + System.getProperty("line.separator")); phoneInfo.append("Line1Number = " + tm.getLine1Number() + System.getProperty("line.separator")); phoneInfo.append("NetworkCountryIso = " + tm.getNetworkCountryIso() + System.getProperty("line.separator")); phoneInfo.append("NetworkOperator = " + tm.getNetworkOperator() + System.getProperty("line.separator")); phoneInfo.append("NetworkOperatorName = " + tm.getNetworkOperatorName() + System.getProperty("line.separator")); phoneInfo.append("NetworkType = " + tm.getNetworkType() + System.getProperty("line.separator")); phoneInfo.append("PhoneType = " + tm.getPhoneType() + System.getProperty("line.separator")); phoneInfo.append("SimCountryIso = " + tm.getSimCountryIso() + System.getProperty("line.separator")); phoneInfo.append("SimOperator = " + tm.getSimOperator() + System.getProperty("line.separator")); phoneInfo.append("SimOperatorName = " + tm.getSimOperatorName() + System.getProperty("line.separator")); phoneInfo.append("SimSerialNumber = " + tm.getSimSerialNumber() + System.getProperty("line.separator")); phoneInfo.append("SimState = " + tm.getSimState() + System.getProperty("line.separator")); phoneInfo.append("SubscriberId(IMSI) = " + tm.getSubscriberId() + System.getProperty("line.separator")); phoneInfo.append("VoiceMailNumber = " + tm.getVoiceMailNumber() + System.getProperty("line.separator")); return phoneInfo.toString(); } }
package com.moziqi.testmvvm; import android.os.Parcel; import android.os.Parcelable; /** * Created by moziqi on 16-4-30. */ public class UserBean implements Parcelable{ private int id; private String name; public UserBean() { } public UserBean(int id, String name) { this.id = id; this.name = name; } protected UserBean(Parcel in) { id = in.readInt(); name = in.readString(); } public static final Creator<UserBean> CREATOR = new Creator<UserBean>() { @Override public UserBean createFromParcel(Parcel in) { return new UserBean(in); } @Override public UserBean[] newArray(int size) { return new UserBean[size]; } }; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(id); parcel.writeString(name); } }
我是基于android studio2.0编写的,具体代码在https://github.com/moziqi/TestMvvm,大家可以比较下,我改动了tiggerokhttp什么代码,代码并不是很多。
谢谢下面的博主提供思路给我
http://blog.csdn.net/huyongl1989/article/details/50467977
http://www.jianshu.com/p/aad5aacd79bf
https://github.com/duzechao/OKHttpUtils