Blame view

C5/txpad/src/main/java/com/sunvote/txpad/server/Api.java 2.98 KB
fac86401   孙向锦   初始化C5 Vote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  package com.sunvote.txpad.server;
  
  import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  import com.sunvote.txpad.Constants;
  import com.sunvote.util.LogUtil;
  
  import java.io.IOException;
  
  import okhttp3.CacheControl;
  import okhttp3.Interceptor;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.Response;
  import retrofit2.Retrofit;
  import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  /**
   * Created by Elvis on 2017/9/12.
   * Email:Eluis@psunsky.com
   * 版权所有:长沙中天电子设计开发有限公司
   * Description:平板模块键盘投票功能模块
   */
  public class Api {
  
      public Retrofit retrofit;
      public ApiService service;
  
      public ApiService getService() {
          return service;
      }
  
      public void setService(ApiService service) {
          this.service = service;
      }
  
      private String url ;
  
      /**
       * 拦截器1
       * 如果后台服务器需要用到token,则每次请求自动对token的添加验证
       * 自动添加请求数据的格式,所有请求URL连接输出
       */
      Interceptor mInterceptor = new Interceptor() {
          @Override
          public Response intercept(Chain chain) throws IOException {
              LogUtil.i("URL","url=="+chain.request().url());
              return chain.proceed(chain.request().newBuilder()
  //                    .addHeader("Token", "Token")
                      .addHeader("Content-Type", "application/json;charset=utf-8")
                      .build());
          }
      };
  
      private Api(){
          OkHttpClient okHttpClient = new OkHttpClient.Builder()
                 /* .readTimeout(7676, TimeUnit.MILLISECONDS)
                  .connectTimeout(7676, TimeUnit.MILLISECONDS)*/
                  .addInterceptor(mInterceptor)
  //                .addInterceptor(interceptor)
                  .addNetworkInterceptor(new HttpCacheInterceptor())
  //                .cache(cache)
                  .build();
  
          Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create();
  
          //initURL();
  
          retrofit = new Retrofit.Builder()
                  .client(okHttpClient)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                  .baseUrl(Constants.BASE_URL)
                  .build();
          service = retrofit.create(ApiService.class);
      }
  
      /**
       *     在访问HttpMethods时创建单例
       */
      private static class SingletonHolder {
          private static final Api INSTANCE = new Api();
      }
  
      /**
       *     获取单例
       */
      public static Api getInstance() {
          return SingletonHolder.INSTANCE;
      }
  
  
      /**
       * 网络缓存
       */
      class HttpCacheInterceptor implements Interceptor {
  
          @Override
          public Response intercept(Chain chain) throws IOException {
              Request request = chain.request();
              return chain.proceed(request);
          }
      }
  }