package com.example.iot_controlhost.utils.network;

import android.widget.SpinnerAdapter;

import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.global.RoomSetting;

import org.xutils.http.RequestParams;

/**
 * @Author：BinJianXin
 * @Date：2022/5/30/9:34
 * @Declaration：
 */
public class XHttpManager {
    private static XHttpManager xHttpManager;

    public static XHttpManager getInstance() {
        if (xHttpManager == null) {
            synchronized (XHttpManager.class) {
                if (xHttpManager == null) {
                    xHttpManager = new XHttpManager();
                }
            }
        }
        return xHttpManager;
    }

    /**
     * 获得网络请求头
     *
     * @param url 地址
     */
    public RequestParams getRequestParams(String url) {
        return getRequestParams(url, false);
    }

    /**
     * 获得网络请求头
     *
     * @param url       地址
     * @param needToken 是否需要token
     */
    public RequestParams getRequestParams(String url, boolean needToken) {
        RequestParams params = new RequestParams(url);
        params.setReadTimeout(10000);
        params.setConnectTimeout(10000);
        params.addHeader("Content-Type", "application/json,charset=UTF-8");
        params.setUseCookie(false);
        params.setAsJsonContent(true);
        if (needToken) {
            params.addHeader("Authorization", MMKVUtil.get(RoomSetting.TOKEN));
        }
        return params;
    }
}
