1.先添加依賴
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
//引入Log攔截器,方便DEBUG模式輸出log信息
? ? compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
? ? compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
2.創(chuàng)建一個接口類(比如下圖)

3.新建application類
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
? ? ? ? loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
? ? ? ? Gson gson = new GsonBuilder()
? ? ? ? ? ? ? ? .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
? ? ? ? ? ? ? ? .create();//使用 gson coverter,統(tǒng)一日期請求格式
? ? ? ? OkHttpClient okHttpClient = new OkHttpClient.Builder()
? ? ? ? ? ? ? ? .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
? ? ? ? ? ? ? ? .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
? ? ? ? ? ? ? ? .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
? ? ? ? ? ? ? ? .addInterceptor(loggingInterceptor)
? ? ? ? ? ? ? ? .cookieJar(new PersistentCookieJar(new SetCookieCache(),new SharedPrefsCookiePersistor(this)))
? ? ? ? ? ? ? ? .build();
? ? ? ? Retrofit retrofit = new Retrofit.Builder()
? ? ? ? ? ? ? ? .baseUrl(Constant.BASE_URL)
? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create(gson))
? ? ? ? ? ? ? ? .client(okHttpClient)
? ? ? ? ? ? ? ? .build();
? ? ? HttpApi mApi = retrofit.create(HttpApi.class);
(mApi抽取成靜態(tài)成員變量,方便直接調(diào)用)
4.Get請求如圖1
? ? Post請求如下圖(分帶參和無參)
