Skip to content

Commit

Permalink
Api整理
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Sep 30, 2020
1 parent c7cba68 commit 41c3d7d
Show file tree
Hide file tree
Showing 41 changed files with 447 additions and 367 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/maning/gankmm/constant/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Constants {

public static final String GANK2_BASEURL = "https://gank.io/api/v2/";

public static final String FIR_BASEURL = "http://api.fir.im/";

//干活历史日期
public static final String URL_HistoryDate = "http://gank.io/api/day/history";

Expand Down
53 changes: 35 additions & 18 deletions app/src/main/java/com/maning/gankmm/http/BuildApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import com.maning.gankmm.app.MyApplication;
import com.maning.gankmm.constant.Constants;
import com.maning.gankmm.http.gank.APIGankService;
import com.maning.gankmm.http.gank2.APIGank2Service;
import com.maning.gankmm.http.mob.APIMobService;
import com.maning.gankmm.http.update.APIUpdateService;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
Expand All @@ -13,29 +19,40 @@
*/
public class BuildApi {

private static Retrofit retrofit;
private static Retrofit gank2_api;
private static Map<String, Retrofit> retrofitMap = new HashMap<>();

public static <T> T getInterface(String baseUrl, Class<T> classs) {
return getRetrofit(baseUrl).create(classs);
}

public static APIService getAPIService() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASEURL) //设置Base的访问路径
.addConverterFactory(GsonConverterFactory.create()) //设置默认的解析库:Gson
.client(MyApplication.defaultOkHttpClient())
.build();
private static Retrofit getRetrofit(String baseUrl) {
if (retrofitMap.containsKey(baseUrl)) {
return retrofitMap.get(baseUrl);
}
return retrofit.create(APIService.class);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(MyApplication.defaultOkHttpClient())
.build();
retrofitMap.put(baseUrl, retrofit);
return retrofitMap.get(baseUrl);
}


public static APIGankService getGankAPIService() {
return getInterface(Constants.BASEURL, APIGankService.class);
}

public static APIMobService getMobAPIService() {
return getInterface(Constants.URL_Mob, APIMobService.class);
}

public static APIGank2Service getGank2APIService() {
if (gank2_api == null) {
gank2_api = new Retrofit.Builder()
.baseUrl(Constants.GANK2_BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.client(MyApplication.defaultOkHttpClient())
.build();
}
return gank2_api.create(APIGank2Service.class);
return getInterface(Constants.GANK2_BASEURL, APIGank2Service.class);
}

public static APIUpdateService getUpdateAPIService() {
return getInterface(Constants.FIR_BASEURL, APIUpdateService.class);
}

}
244 changes: 0 additions & 244 deletions app/src/main/java/com/maning/gankmm/http/GankApi.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maning.gankmm.http;
package com.maning.gankmm.http.callback;

import java.util.List;

Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/maning/gankmm/http/gank/APIGankService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.maning.gankmm.http.gank;

import com.maning.gankmm.bean.DayEntity;
import com.maning.gankmm.bean.HttpResult;
import com.maning.gankmm.constant.Constants;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Path;


/**
* 接口调用的工具类
*/
public interface APIGankService {

//这里填写全部路径就会覆盖掉Build得BaseUrl
@Headers("Cache-Control: public, max-age=3600")
@GET(Constants.URL_HistoryDate)
Call<HttpResult<List<String>>> getGankHistoryDate();

//http://gank.io/api/day/2015/08/06 --- 每日数据
@Headers("Cache-Control: public, max-age=300")
@GET("day/{year}/{month}/{day}")
Call<DayEntity> getOneDayData(@Path("year") String year,
@Path("month") String month,
@Path("day") String day
);

}
Loading

0 comments on commit 41c3d7d

Please sign in to comment.