/**
  * Copyright 2017 JessYan
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
package me.jessyan.mvparms.demo.app;

import android.content.Context;
import android.text.TextUtils;

import com.jess.arms.http.GlobalHttpHandler;
import com.jess.arms.http.RequestInterceptor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;

/**
 * ================================================
 * 展示 {@link GlobalHttpHandler} 的用法
 * <p>
 * Created by JessYan on 04/09/2017 17:06
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class GlobalHttpHandlerImpl implements GlobalHttpHandler {
    private Context context;

    public GlobalHttpHandlerImpl(Context context) {
        this.context = context;
    }

    @Override
    public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response) {
                    /* 这里可以先客户端一步拿到每一次http请求的结果,可以解析成json,做一些操作,如检测到token过期后
                       重新请求token,并重新执行请求 */
        try {
            if (!TextUtils.isEmpty(httpResult) && RequestInterceptor.isJson(response.body().contentType())) {
                JSONArray array = new JSONArray(httpResult);
                JSONObject object = (JSONObject) array.get(0);
                String login = object.getString("login");
                String avatar_url = object.getString("avatar_url");
                Timber.w("Result ------> " + login + "    ||   Avatar_url------> " + avatar_url);
            }

        } catch (JSONException e) {
            e.printStackTrace();
            return response;
        }


                 /* 这里如果发现token过期,可以先请求最新的token,然后在拿新的token放入request里去重新请求
                    注意在这个回调之前已经调用过proceed,所以这里必须自己去建立网络请求,如使用okhttp使用新的request去请求
                    create a new request and modify it accordingly using the new token
                    Request newRequest = chain.request().newBuilder().header("token", newToken)
                                         .build();

                    retry the request

                    response.body().close();
                    如果使用okhttp将新的请求,请求成功后,将返回的response  return出去即可
                    如果不需要返回新的结果,则直接把response参数返回出去 */

        return response;
    }

    // 这里可以在请求服务器之前可以拿到request,做一些操作比如给request统一添加token或者header以及参数加密等操作
    @Override
    public Request onHttpRequestBefore(Interceptor.Chain chain, Request request) {
                    /* 如果需要再请求服务器之前做一些操作,则重新返回一个做过操作的的request如增加header,不做操作则直接返回request参数
                       return chain.request().newBuilder().header("token", tokenId)
                              .build(); */
        return request;
    }
}

@@ -702,16 +702,58 @@
 Utils;%0A%0A
+import com.google.gson.reflect.TypeToken;%0A
 import c
@@ -839,95 +839,124 @@
 or;%0A
-%0A
 import 
-org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A
+com.jess.arms.utils.ArmsUtils;%0A%0Aimport java.util.List;%0A%0Aimport me.jessyan.mvparms.demo.mvp.model.entity.User;
 %0Aimp
@@ -1806,26 +1806,9 @@
  */%0A
-        try %7B%0A    
+%0A
     
@@ -1925,243 +1925,206 @@
     
-    JSONArray array = new JSONArray(httpResult);%0A                JSONObject object = (JSONObject) array.get(0);%0A                String login = object.getString(%22login%22);%0A                String avatar_url = object.getString(%22avatar_url%22
+try %7B%0A                List%3CUser%3E list = ArmsUtils.obtainAppComponentFromContext(context).gson().fromJson(httpResult, new TypeToken%3CList%3CUser%3E%3E() %7B%7D.getType());%0A                User user = list.get(0
 );%0A 
@@ -2171,13 +2171,23 @@
 %22 + 
-l
+user.getL
 ogin
+()
  + %22
@@ -2221,18 +2221,27 @@
 %22 + 
-avatar_url
+user.getAvatarUrl()
 );%0A 
@@ -2256,27 +2256,16 @@
    %7D
-%0A%0A        %7D
  catch (
 JSON
@@ -2264,12 +2264,8 @@
 ch (
-JSON
 Exce
@@ -2287,16 +2287,20 @@
         
+    
 e.printS
@@ -2316,32 +2316,36 @@
 ();%0A            
+    
 return response;
@@ -2353,18 +2353,31 @@
         
+   
+ 
 %7D%0A
+        %7D
 %0A%0A      

Improve GlobalHttpHandlerImpl