OkDroid

Network request library based on okHTTP secondary encapsulation, supporting GET request, POST request, file upload, file download, cancel request, support Json, Gson, Raw data type return result callback processing.

rendering

reference

1. github.com/zhaokaiqian… 2. github.com/hongyangand…

Introduction to the

Add dependencies to build.gradle in the project app directory

The compile 'com. MPH. Okdroid: okdroid: 1.0.0'Copy the code

1. Create a unique OkDroid instance in the project entry

OkDroid okDroid = new OkDroid(); okDroid.setDebug(true); // Enable logCopy the code

If you need to configure connection timeout, cookies, etc., you can create the OkDroid constructor:

OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .build(); OkDroid okDroid = new OkDroid(okHttpClient); okDroid.setDebug(true); // Enable logCopy the code

2. Add parameters

A. You can add a param parameter by using addParam or multiple params. B. You can add a single request header using addHeader or add headers using headers.

3. Result callback support

A. Son support, callback passed to GsonResHandler

B. Son support, callback passed to JsonResHandler c. RawSupport, callback passed to RawResHandler

Simple to use

GET + RawResHandler

Okdroid.get ().url(" request address ").tag(this).enqueue(new RawResHandler() {@override public void onSuccess(int statusCode, String response) { Log.d(GetActivity.class.getSimpleName(),response); } @Override public void onFailed(int statusCode, String errMsg) { } });Copy the code

POST + JsonResHandler

Okdroid.post ().url(" request address ").tag(this).addparam ("type","1").enqueue(new JsonResHandler() {@override public void onSuccess(int statusCode, JSONObject response) { if(response! =null){ if(! TextUtils.isEmpty(response.toString())){ mTvContent.setText(response.toString()); } } } @Override public void onFailed(int statusCode, String errMsg) { } });Copy the code

File upload

Map<String, String> map = new HashMap<>(); map.put("address", ""); map.put("gender", ""); map.put("height", "42"); map.put("weight", "21"); map.put("realname", "fsd"); map.put("waist", "43"); map.put("userid", "285"); File file = new File("/sdcard/images/20170308_131947.jpg"); Okdroid.upload ().url(" request address ").tag(UploadActivity. This).params(map).addfile ("avatarByte", file) .enqueue(new JsonResHandler() { @Override public void onProgress(long progress, Long total) {Log. D (UploadActivity. Class. GetSimpleName (), "the current progress -- >" + progress + ", total size -- -- > "+ total); } @Override public void onSuccess(int statusCode, JSONObject response) { Log.d(UploadActivity.class.getSimpleName(), response.toString()); } @Override public void onFailed(int statusCode, String errMsg) { } });Copy the code

The download file

okDroid.download().url("http://ivy.pconline.com.cn/click?adid=434690&id=pc.xz.android.zd.tl1.&__uuid=10220796") .tag(this) .filePath(Environment.getExternalStorageDirectory()+"/okdroid/kyw.apk") .enqueue(new IResponseDownloadHandler() { @Override public void onFinish(File downloadFile) { The d (DownloadActivity. Class. GetSimpleName (), "download success"); } @Override public void onProgress(long progress, long total) { Log.d(DownloadActivity.class.getSimpleName(), "+ progress +" -->" + total); } @Override public void onFailed(String errMsg) { } @Override public void onCancel() { } });Copy the code

Note: In the above example, the requested tag is this, so the request can be cancelled as follows:

okDroid.cancel(this);Copy the code

confusion

#okdroid -dontwarn com.mph.okdroid.** -keep class com.mph.okdroid.**{*; } #okhttp -dontwarn okhttp3.** -keep class okhttp3.**{*; } #okio -dontwarn okio.** -keep class okio.**{*; }Copy the code

About Me

ivast.cn