/**
  * 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.mvpart.demo.app;

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

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

import me.jessyan.art.http.GlobalHttpHandler;
import me.jessyan.art.http.RequestInterceptor;
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;
    }
}

@@ -1,13 +1,12 @@
 /**%0A
- 
  * Copyr
@@ -24,20 +24,22 @@
 essYan%0A 
- *%0A 
+* %3Cp%3E%0A
  * Licen
@@ -97,17 +97,16 @@
 ense%22);%0A
- 
  * you m
@@ -165,17 +165,16 @@
 icense.%0A
- 
  * You m
@@ -213,19 +213,16 @@
 at%0A 
- *%0A  *     
+* %3Cp%3E%0A *
  htt
@@ -266,12 +266,14 @@
 .0%0A 
- *%0A 
+* %3Cp%3E%0A
  * U
@@ -339,17 +339,16 @@
 oftware%0A
- 
  * distr
@@ -408,17 +408,16 @@
  BASIS,%0A
- 
  * WITHO
@@ -484,17 +484,16 @@
 mplied.%0A
- 
  * See t
@@ -555,17 +555,16 @@
 ons and%0A
- 
  * limit
@@ -589,17 +589,16 @@
 icense.%0A
- 
  */%0Apack
@@ -705,84 +705,64 @@
 ort 
-org.json.JSONArray;%0Aimport org.json.JSONExceptio
+com.google.gson.reflect.TypeToke
 n;%0A
+%0A
 import 
-org.json.JSONObjec
+java.util.Lis
 t;%0A%0A
@@ -846,32 +846,123 @@
 estInterceptor;%0A
+import me.jessyan.art.utils.ArtUtils;%0Aimport me.jessyan.mvpart.demo.mvp.model.entity.User;%0A
 import okhttp3.I
@@ -1799,26 +1799,9 @@
  */%0A
-        try %7B%0A    
+%0A
     
@@ -1918,243 +1918,205 @@
     
-    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 = ArtUtils.obtainAppComponentFromContext(context).gson().fromJson(httpResult, new TypeToken%3CList%3CUser%3E%3E() %7B%7D.getType());%0A                User user = list.get(0
 );%0A 
@@ -2163,13 +2163,23 @@
 %22 + 
-l
+user.getL
 ogin
+()
  + %22
@@ -2213,18 +2213,27 @@
 %22 + 
-avatar_url
+user.getAvatarUrl()
 );%0A 
@@ -2248,27 +2248,16 @@
    %7D
-%0A%0A        %7D
  catch (
 JSON
@@ -2256,12 +2256,8 @@
 ch (
-JSON
 Exce
@@ -2279,16 +2279,20 @@
         
+    
 e.printS
@@ -2308,32 +2308,36 @@
 ();%0A            
+    
 return response;
@@ -2345,18 +2345,31 @@
         
+   
+ 
 %7D%0A
+        %7D
 %0A%0A      

Improve GlobalHttpHandlerImpl