Skip to content

Commit

Permalink
Add @nonnull on GlobalHttpHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
JessYanCoding committed Dec 11, 2018
1 parent 42ddb04 commit 7688353
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
42 changes: 34 additions & 8 deletions arms/src/main/java/com/jess/arms/http/GlobalHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.jess.arms.http;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.jess.arms.di.module.GlobalConfigModule;

import okhttp3.Interceptor;
Expand All @@ -33,23 +36,46 @@
* ================================================
*/
public interface GlobalHttpHandler {
Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response);

Request onHttpRequestBefore(Interceptor.Chain chain, Request request);
/**
* 这里可以先客户端一步拿到每一次 Http 请求的结果, 可以先解析成 Json, 再做一些操作, 如检测到 token 过期后
* 重新请求 token, 并重新执行请求
*
* @param httpResult 服务器返回的结果 (已被框架自动转换为字符串)
* @param chain {@link okhttp3.Interceptor.Chain}
* @param response {@link Response}
* @return {@link Response}
*/
@NonNull
Response onHttpResultResponse(@Nullable String httpResult, @NonNull Interceptor.Chain chain, @NonNull Response response);

/**
* 这里可以在请求服务器之前拿到 {@link Request}, 做一些操作比如给 {@link Request} 统一添加 token 或者 header 以及参数加密等操作
*
* @param chain {@link okhttp3.Interceptor.Chain}
* @param request {@link Request}
* @return {@link Request}
*/
@NonNull
Request onHttpRequestBefore(@NonNull Interceptor.Chain chain, @NonNull Request request);

//空实现
/**
* 空实现
*/
GlobalHttpHandler EMPTY = new GlobalHttpHandler() {

@NonNull
@Override
public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response) {
//不管是否处理,都必须将response返回出去
public Response onHttpResultResponse(@Nullable String httpResult, @NonNull Interceptor.Chain chain, @NonNull Response response) {
//不管是否处理, 都必须将 response 返回出去
return response;
}

@NonNull
@Override
public Request onHttpRequestBefore(Interceptor.Chain chain, Request request) {
//不管是否处理,都必须将request返回出去
public Request onHttpRequestBefore(@NonNull Interceptor.Chain chain, @NonNull Request request) {
//不管是否处理, 都必须将 request 返回出去
return request;
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package me.jessyan.mvparms.demo.app;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -52,12 +54,13 @@ public GlobalHttpHandlerImpl(Context context) {
* 重新请求 token, 并重新执行请求
*
* @param httpResult 服务器返回的结果 (已被框架自动转换为字符串)
* @param chain {@link okhttp3.Interceptor.Chain}
* @param response {@link Response}
* @return
* @param chain {@link okhttp3.Interceptor.Chain}
* @param response {@link Response}
* @return {@link Response}
*/
@NonNull
@Override
public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response) {
public Response onHttpResultResponse(@Nullable String httpResult, @NonNull Interceptor.Chain chain, @NonNull Response response) {
if (!TextUtils.isEmpty(httpResult) && RequestInterceptor.isJson(response.body().contentType())) {
try {
List<User> list = ArmsUtils.obtainAppComponentFromContext(context).gson().fromJson(httpResult, new TypeToken<List<User>>() {
Expand Down Expand Up @@ -87,12 +90,13 @@ public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain,
/**
* 这里可以在请求服务器之前拿到 {@link Request}, 做一些操作比如给 {@link Request} 统一添加 token 或者 header 以及参数加密等操作
*
* @param chain {@link okhttp3.Interceptor.Chain}
* @param chain {@link okhttp3.Interceptor.Chain}
* @param request {@link Request}
* @return
* @return {@link Request}
*/
@NonNull
@Override
public Request onHttpRequestBefore(Interceptor.Chain chain, Request request) {
public Request onHttpRequestBefore(@NonNull Interceptor.Chain chain, @NonNull Request request) {
/* 如果需要在请求服务器之前做一些操作, 则重新构建一个做过操作的 Request 并 return, 如增加 Header、Params 等请求信息, 不做操作则直接返回参数 request
return chain.request().newBuilder().header("token", tokenId)
.build(); */
Expand Down

0 comments on commit 7688353

Please sign in to comment.