pre-network Preloading Network Framework

Keywords: network Java OkHttp

Network optimization is a very important part of all app development. If the network request is pre-loaded before clicking on jump activity, the speed will be improved qualitatively. That is, the network preload frame.

Network preload framework, listening network preload framework - Network preload, network preload the framework.- pre-network

Framework description

pre-network: A network pre-loading framework based on observer mode can optimize the network loading speed to a great extent; each network request is equivalent to the observer, as long as the subscribed observer can get the observer's instance.

Basic usage method

Adding dependencies:

implementation 'com.liyihang:pre-network:1.2.2'

Every observer network must have a string unique id to identify him, and the observer subscription is also through the unique id to find the observer and then subscribe.
Here, for example, an i web_data requested by an observer;

First, create the observer network request;
The code is as follows:

PreNetworkHelper.getInstance()
                    .removeRequestObservable("web_data") // First of all, it is clear that the previous observer request for the same id is removed, and there is no way to add in if the same id request is not removed.
                    .addRequestObservable(new PNQuickRequest("web_data", "http://baidu.com/"));// Add the observee and execute the network request immediately after adding it.

PNQuickRequest is a simple encapsulation of the observee; you can see the code:

package com.prenetwork.liyihang.lib_pre_network;

import java.util.Map;

/**
 * Created by liyihang on 18-1-16.
 */

public class PNQuickRequest extends PNRequestObservable {

    private String id;
    private String url;
    private String parms;
    private Map<String, String> header;

    public PNQuickRequest(String id, String url) {
        this.id = id;
        this.url = url;
        parms=null;
        header=null;
    }

    public PNQuickRequest(String id, String url, String parms) {
        this.id = id;
        this.url = url;
        this.parms = parms;
        header=null;
    }

    public PNQuickRequest(String id, String url, String parms, Map<String, String> header) {
        this.id = id;
        this.url = url;
        this.parms = parms;
        this.header = header;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public Map<String, String> getRequestHeader() {
        return header;
    }

    @Override
    public String getRequestParms() {
        return parms;
    }

    @Override
    public String getRequestUrl() {
        return url;
    }

}

ID is the unique id, url network request address, parm network request parameter, header network request header field.

How to subscribe to the request code is as follows:

PreNetworkHelper.getInstance().addObserver(new PNObserver() {
            @Override
            public void call(PNRequestObservable observable) {
                final String result = observable.getResult();// Get the content of the network request, which occurs in non-ui threads. observable is an example of the observee
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //... do samething
                    }
                });
            }

            @Override
            public String getId() {
                return "web_data";// unique id
            }
        }).removeRequestObservable("web_data");// When the request is exhausted, it can be removed. If it is not removed, it will remain in memory. Note: This step goes to addObserver. 

In depth use

Basically, companies develop app s that encapsulate their own network request framework. pre-network uses HTTP URLConnection, which is the basis of network requests:

        executor.submit(new Runnable() {
            @Override
            public void run() {
                if ("GET".equals(getRequestMethod()))
                    result = PNGetPostUtil.sendGet(getRequestUrl(), getRequestParms(), getRequestHeader());

                if ("POST".equals(getRequestMethod()))
                    result = PNGetPostUtil.sendPost(getRequestUrl(), getRequestParms(), getRequestHeader());

                dataChange();
            }
        });

Then, with your own company's network requests, you first need to create a request body that implements the PNRequestObservable class. Here's an example of inheriting okhttp:

/**
 * Created by liyihang on 18-1-17.
 */

public class MyRequestObservable extends PNRequestObservable {

    private static final OkHttpClient HTTP_CLIENT=new OkHttpClient();

    @Override
    public String getId() {
        return "request_id";// unique id
    }

    @Override
    public Map<String, String> getRequestHeader() {
        return null;
    }

    @Override
    public String getRequestParms() {
        return null;
    }

    @Override
    public String getRequestUrl() {
        return null;
    }

    @Override
    public void handlerRequest() {
//        Sup. handlerRequest (); Turn off the original network request processing method
        Request.Builder builder=new Request.Builder();
        builder.url("https://www.baidu.com/");
        HTTP_CLIENT.newCall(builder.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                //When the network is finished, it must be called
                requestPost(null);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String data = response.body().string();

                // do samething......

                //When the network is finished, it must be called
                requestPost(data);
            }
        });
    }
}

Call methods such as:

        PreNetworkHelper.getInstance()
                .removeAllObservable()// Delete all requests
                .addRequestObservable(new MyRequestObservable())// Adding requests and execution
                .addObserver(new PNObserver() { //Add callbacks
                    @Override
                    public void call(PNRequestObservable observable) {
                        MyRequestObservable result= (MyRequestObservable) observable;
                        PNUtils.msg("end:"+result.getResult());
                    }

                    @Override
                    public String getId() {
                        return "request_id";
                    }
                })
                .removeAllObservable();

Author: Yihang

Posted by lilman on Wed, 15 May 2019 22:58:24 -0700