gpt4 book ai didi

java - Retrofit2 MVP 安卓

转载 作者:行者123 更新时间:2023-11-29 08:26:46 25 4
gpt4 key购买 nike

我正在对我的项目进行改造,我想知道是否有一种方法可以将对不同类的 api 调用分开,例如:仅限登录 Activity /api/users/login仅限电影 Activity /api/movies/all我都在同一个界面上,我发现这不是一个好方法......你能指导我如何让它更干净吗?我正在使用 MVP 架构来清理它。

这是我的NetworkService.class

public class NetworkService {

private NetworkAPI networkAPI;
private OkHttpClient okHttpClient;
private LruCache<Class<?>, Observable<?>> apiObservables;

public NetworkService() {
this(BASE_URL);
}

public NetworkService(String baseUrl) {
okHttpClient = buildClient();
apiObservables = new LruCache<>(10);

Gson gson = new GsonBuilder()
.setLenient()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
//logging.setLevel(Level.BASIC);
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

Builder httpClient = new Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS);

httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();

networkAPI = retrofit.create(NetworkAPI.class);
}

/**
* Method to return the API interface.
*
* @return
*/
public NetworkAPI getAPI() {
return networkAPI;
}


/**
* Method to build and return an OkHttpClient so we can set/get
* headers quickly and efficiently.
*
* @return
*/
public OkHttpClient buildClient() {

Builder builder = new Builder();

builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
// Do anything with response here
//if we want to grab a specific cookie or something..
return response;
}
});

builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
//this is where we will add whatever we want to our request headers.
Request request = chain.request().newBuilder().addHeader("Accept", "application/json").build();
return chain.proceed(request);
}
});

return builder.build();
}

/**
* Method to clear the entire cache of observables
*/
public void clearCache() {
apiObservables.evictAll();
}
}

我的 NetworkAPI.class 有这个

public interface NetworkAPI {


@POST(LOGIN)
Call<LoginResponse> login(@Body LoginRequest loginRequest);
//And more calls...
}

如果我可以做得更干净,你们知道吗?

最佳答案

制作 retrofit 实例单例,然后您可以为登录电影创建服务,例如

   LoginService login =  retrofit.create(LoginService.class);
login.login(loginRequest)

MovieService movie = retrofit.create(MovieService.class);
movie.movies(movieRequest)

你的界面是这样的

public interface LoginService {


@POST(LOGIN)
Call<LoginResponse> login(@Body LoginRequest loginRequest);
//And more calls...
}

public interface MovieService {


@POST(MOVIE)
Call<MovieResponse> movies(@Body MovieRequest movieRequest);
//And more calls...
}

关于java - Retrofit2 MVP 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52096746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com