Version 2021

Add Customized Headers to Network Requests in Android

You can customize the information sent in a network request for Android. For example, you can add customized HTTP headers to the request. Customized request headers are useful for passing additional information in the request. For example, you may want to add special authentication information, such as Good Dynamics tokens, to the headers of HTTP requests. The CustomizedHttpHeader class is responsible for carrying customized header values to every request.

APIs

Copy
/**
 * This class provides a singleton instance to set/get/clear customized network headers.
 *
 * */
public class CustomizedHttpHeader {

    private HashMap<String, String> headerDict;
    private static CustomizedHttpHeader sCustomizedHttpHeader;

    private CustomizedHttpHeader() {
        if(headerDict == null) {
            headerDict = new HashMap<String, String>();
        }
    }

    /**
     * This method provides the singleton instance of this class
     *
     * @return the singleton instance of this class
     *
     * */
    public synchronized static CustomizedHttpHeader sharedCustomizedHeader(){
        if(sCustomizedHttpHeader == null){
            sCustomizedHttpHeader = new CustomizedHttpHeader();
        }
        return sCustomizedHttpHeader;
    }

    /**
     * This method provides all the network header keys.
     *
     * @return a String Array which holds all the customized network header keys.
     * */

    public String[] getHttpHeaderFields(){
        Set<String> keySet = headerDict.keySet();
        return keySet.toArray(new String[keySet.size()]);
    }

    /**
     * Giving a specific customized network header, return the value.
     *
     * @param field the String of the customized network header.
     * @return value for the customized network header. return null if the header doesn't exist.
     * */

    public String getValueForHttpHeaderField(String field) {
        return headerDict.get(field);
    }

    /**
     * Giving a specific customized network header, remove this item.
     *
     * @param field the String of the customized network header.
     * */
    public void removeValueForHttpHeaderField(String field){
        headerDict.remove(field);
    }

    /**
     * This method is used to insert one item in customized header dictionary.
     *
     * @param field the String of the customized network header.
     * @param value value for the customized network header you want to set.
     * */
    public void setValueForHttpHeaderField(String value, String field){
        if(field != null && value != null && !field.isEmpty() ) {
            headerDict.put(field, value);
        }
    }

    /**
     * This method is used to clear all the customized headers.
     * */
    public void clearHttpHeaderFields(){
        headerDict.clear();
    }

    /**
     * This method is used to get the size of all the customized headers.
     *
     * @return the count of the customized headers
     * */
    public int sizeOfHttpHeaderFields(){
        return headerDict.size();
    }

    /**
     * This method is used to get the map of all the customized headers.
     *
     * @return the hashMap of the customized headers.
     * */
    public HashMap<String,String> getHeaderMap() {
        return headerDict;
    }
}

Internal Logic

All of the customized HTTP headers are merged into final request in HttpClientManager.

Copy
 //execute http request
    public static Response execute(OkHttpClient okHttpClient, HttpReq req, Map<String,String> headers) throws IOException {
        //Put customized headers into request.
        headers.putAll(CustomizedHttpHeader.sharedCustomizedHeader().getHeaderMap());
        Request request = req.getRequestType() == HttpReq.HttpRequestType.POST ? createOkHttpPost(req,headers) :
                createOkHttpGet(req,headers);

        Call requestCall = okHttpClient.newCall(request);
        return requestCall.execute();
    }

APIs and Usage

Download the Android SDK and use the CustomizedHttpHeader singleton instance to store customized headers and values as shown in the code snippet below.

Copy
public class CustomizedHttpHeaderTest {

    //Clear all the Customized HTTP headers
    public void testClearHttpHeaderFields(){
        CustomizedHttpHeader shared = CustomizedHttpHeader.sharedCustomizedHeader();
        shared.clearHttpHeaderFields();
    }

    //Set customized value for "SM_USER" header
    public void testSetValueForHttpHeaderField() {
        CustomizedHttpHeader shared = CustomizedHttpHeader.sharedCustomizedHeader();
        String field = "SM_USER";
        String value = "test";
        shared.setValueForHttpHeaderField(value,field);
    }

    //Get customized value for "SM_USER" header
    public void testGetValueForHttpHeaderField() {
        CustomizedHttpHeader shared = CustomizedHttpHeader.sharedCustomizedHeader();
        String value = shared.getValueForHttpHeaderField("SM_USER");
    }

    //Get the size of all the customized headers
    public void testSizeOfHttpHeaderFields() {
        CustomizedHttpHeader shared = CustomizedHttpHeader.sharedCustomizedHeader();
b       int size = shared.sizeOfHttpHeaderFields();
    }
    //Remove the value of "SM_USER" header
    public void testRemoveValueForHttpHeaderField() {
        CustomizedHttpHeader shared = CustomizedHttpHeader.sharedCustomizedHeader();
        shared.removeValueForHttpHeaderField("SM_USER");
    }
    //Get all the customized headers.
    public void testGetHttpHeaderFields() {
        CustomizedHttpHeader shared = CustomizedHttpHeader.sharedCustomizedHeader();
        String[] rs = shared.getHttpHeaderFields();
    }
}