第三方请求头

创建SpUtils

package jiaoyibo.bawei.com.retrofitdenglu.net;

import android.content.Context;
import android.content.SharedPreferences;

public class SpUtils {
    private static final String FILE_NAME = "user";
    private static SharedPreferences mSharedPreferences;// 单例
    private static SpUtils instance;// 单例

    private SpUtils(Context context) {
        mSharedPreferences = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
    }
    /**
     * 初始化单例
     * @param context
     */
    public static synchronized void init(Context context) {
        if (instance == null) {
            instance = new SpUtils(context);
        }
    }

    /**
     * 获取单例
     *
     * @return
     */
    public static SpUtils getInstance() {
        if (instance == null) {
            throw new RuntimeException("class should init!");
        }
        return instance;
    }

    /**
     * 保存数据
     *
     * @param key
     * @param data
     */
    public void saveData(String key, Object data) {
        String type = data.getClass().getSimpleName();

        SharedPreferences.Editor editor = mSharedPreferences.edit();

        if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) data);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) data);
        } else if ("String".equals(type)) {
            editor.putString(key, (String) data);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) data);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) data);
        }

        editor.commit();
    }

    /**
     * 得到数据
     *
     * @param key
     * @param defValue
     * @return
     */
    public Object getData(String key, Object defValue) {

        String type = defValue.getClass().getSimpleName();
        if ("Integer".equals(type)) {
            return mSharedPreferences.getInt(key, (Integer) defValue);
        } else if ("Boolean".equals(type)) {
            return mSharedPreferences.getBoolean(key, (Boolean) defValue);
        } else if ("String".equals(type)) {
            return mSharedPreferences.getString(key, (String) defValue);
        } else if ("Float".equals(type)) {
            return mSharedPreferences.getFloat(key, (Float) defValue);
        } else if ("Long".equals(type)) {
            return mSharedPreferences.getLong(key, (Long) defValue);
        }

        return null;
    }
}

创建QinqiuUtils

package jiaoyibo.bawei.com.retrofitdenglu.net;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class QinqiuUtils implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Object userId = SpUtils.getInstance().getData("userId", "");
        Object sessionId = SpUtils.getInstance().getData("sessionId", "");
        Request.Builder builder = request.newBuilder();
        builder.addHeader("userId", (String) userId);
        builder  .addHeader("sessionId", (String) sessionId);
        Request build = builder.build();
        Response C = chain.proceed(build);
        return C;
    }
}

记得app初始化


 SpUtils.init(getApplicationContext());

在工具类里OkHttpClient里添加

.addInterceptor(new QinqiuUtils())

登录成功方法调用

SpUtils.getInstance().saveData("userId",loginBean.getResult().getUserId()+"");
SpUtils.getInstance().saveData("sessionId",loginBean.getResult().getSessionId()+"");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值