Effect:

Project Description:

Secondary packaging network request frame, now completed based on OkHttp get/post/postJson/uploadFile/downloadFile function such as development, support the extension, the underlying implementation can switch freely;
  • ExecutorFactory extensions can be implemented as an IExecutor, which can be OkHttp, HttpClient, or URLConnection.

Usage:

public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Init (new OkHttpExecutorFactory()); // Init (new OkHttpExecutorFactory()); }} / / the get method request private void doGet () {String url = "http://127.0.0.1:8080/test/test"; RequestParams params = new RequestParams.Builder().url(url).method(Method.GET).params("key1", "hello").params("key2", "doGet").tag(this).build(); NetWorkUtils.getInstance().doStart(params, callback); } / / post requests private void the doPost () {String url = "http://127.0.0.1:8080/test/test"; RequestParams params = new RequestParams.Builder().url(url).method(Method.POST).params("key1", "hello").params("key2", "doPost").tag(this).build(); NetWorkUtils.getInstance().doStart(params, callback); } / / postJson way request private void doPostJson () {String url = "http://127.0.0.1:8080/test/test"; String body = "{ \"key1\":\"hello\",\"key2\":\"doPostJson\" }"; RequestParams params = new RequestParams.Builder().url(url).method(Method.POST_JSON).body(body).tag(this).build(); NetWorkUtils.getInstance().doStart(params, callback); } / / file upload private void doUploadFile () {String url = "http://127.0.0.1:8080/test/upload"; String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "atlas-master.zip"; RequestParams params = new RequestParams.Builder().url(url).method(Method.UPLOAD).files("fileKey", new File(filePath)).tag(this).build(); NetWorkUtils.getInstance().doStart(params, callback); } / / file download private void doDownload () {String url = "http://127.0.0.1:8080/test/download"; String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "atlas-master.zip"; RequestParams params = new RequestParams.Builder().url(url).method(Method.DOWNLOAD).downLoadFilePath(filePath).tag(this).build(); NetWorkUtils.getInstance().doStart(params, callback); } // Request a callback, Private ICallBack callback = new ICallBack() {@override public void onStart() { The d (NetWorkUtils. Class. GetSimpleName (), "the network began to load"); {} @ Override public void onComplite () the d (NetWorkUtils. Class. GetSimpleName (), "network loaded"); } @ Override public void onError (Exception e) {the d (NetWorkUtils. Class. GetSimpleName (), "error"); } @Override public void onSuccess(Object tag, String result) { Log.d(NetWorkUtils.class.getSimpleName(), "result = "+result); / * * * can parse the data according to different tag * if (tag. The equest (" doGet ")) {} * * else if (tag. The equest (" doPost ")) {} * * *... * /} @ Override public void onProgress (float progress) {the d (NetWorkUtils. Class. GetSimpleName (), "upload or download progress: "+progress); }}; Protected void onDestroy() {networkutils.getInstance ().canclerequest (this); super.onDestroy(); }Copy the code

Technical points:

  • Abstract Factory pattern
  • Builder pattern
  • OkHttp use
  • OkHttp upload and download progress monitor
  • Thread the callback

Background:

Network request plays a very important role in APP development and is also one of the core technical points. There are also many well-known open source libraries on Github, such as Xutils, AsyncHttpClient, Okhttp, etc. I’m sure many of you, like me, habitually add these open source versions to your projects, but one day, for some reason, we need to make a switch to the network layer, then the problem becomes serious and we need to change a lot of code to accommodate this change. To avoid such problems, we need to re-wrap the network layer;

Project Structure:

  • IExecutor request network interface class, defines the get/post/postJson/uploadFile/downloadFile, etc.;
  • ExecutorFactory Creation factory of IExecutor, abstract class;
  • OkHttpExecutor realized IExecutor interface to achieve the specific get/post/postJson/uploadFile/downloadFile method;
  • OkHttpExecutorFactory implements ExecutorFactory and returns an OkHttpExecutor instance;
  • MainHandler UI thread callback;
  • RequestParams request parameters, using the builder pattern;
  • NetWorkUtils Network request utility class, simple interest, globally unique.
Note: the default okHTTP enqueue (call) is followed by the callback in the subline;

Process execution, code analysis:

Well, the introduction of almost, have a problem friends welcome to exchange, the group has a buddy said a word, every day does not progress, is regressive. Let’s come on!

Demo, source code, server code are on Github, welcome startGithub.com/andmizi/Net…

Jane Book blog:www.jianshu.com/p/ec519e1c1…