在Android项目中使用Retrofit库的高效方法是什么?

时间:2016-03-29 04:03:10

标签: android retrofit

我使用Retrofit库在我的Android项目中实现网络任务。我很困惑,使用Retrofit的高效方法是什么。

我在项目中创建了两个类,一个是 API.java ,另一个是 Route.java

API.java

public class Api {

public static final String BASE_URL = "";

private static Api instance = new Api();

public static Api getInstance() {
    return instance;
}

private Api(){}

public Retrofit getRetrofit() {
    HttpLoggingInterceptor interceptor1 = new HttpLoggingInterceptor();
   interceptor1.setLevel(HttpLoggingInterceptor.Level.BODY);
    LogJsonInterceptor interceptor2 = new LogJsonInterceptor();

    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor1).addInterceptor(interceptor2)
            //.addInterceptor(interceptor1).addInterceptor(headerINterceptor)
            .retryOnConnectionFailure(true)
            .connectTimeout(15, TimeUnit.SECONDS)
            .build();

    // set gson converter lenient mode
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .setLenient()
            .create();

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

    return retrofit;
}

public <S> S createService(Class<S> serviceClass) {
    return getRetrofit().create(serviceClass);
}
}

Routes.java

public interface Routes {
/*-------------------------------------------------------------event  -------------------------------------------------------*/
@GET("/events")
public Call<ApiResult<List<Event>>> getRecommendedEvents(@Query("start") int id, @Query("per_page") int count);

@POST("/event/new")
public Call<ApiResult<Event>> createEvent(@Body Event newEvent);

@POST("/published_event")
public Call<ApiResult<List<PublishedEventDetail>>> getPublishedEventDetail(@Query("user_id") int user_id, @Query("start") int id, @Query("per_page") int count);

/*-------------------------------------------------------- user -------------------------------------------------------*/
@POST("/users/new")
public Call<ApiResult<User>> createUser(@Body User user);

@POST("/users/login")
public Call<ApiResult<User>> login(@Body User user);

@POST("/users/update")
public Call<ApiResult<User>> update(@Body User user);

@POST("/users/new_guest")
public Call<ApiResult<User>> create_guest();

/*-------------------------------------------------------event registration -------------------------------------------------------*/
@POST("/event_registration/add")
public Call<ApiResult<UserEventDetail>> addEventRegistration(@Body UserEventDetail userEventDetail);

@GET("/event_registration/delete")
public Call<ApiResult<String>> deleteEventRegistration(@Query("user_id") int userId, @Query("event_id") int eventId);

@GET("/event_registration")
public Call<ApiResult<List<Event>>> getRegisteredEvents(@Query("user_id") int user_id, @Query("start") int id, @Query("per_page") int count);

@GET("/registered_user")
public Call<ApiResult<List<RegisteredUser>>> getRegisteredUsers(@Query("event_id") int user_id, @Query("start") int id, @Query("per_page") int count);

/*--------------------------------------------------------event  view -------------------------------------------------------*/
@GET("/event_view")
public Call<ApiResult<List<Event>>> getViewedEvents(@Query("user_id") int user_id, @Query("start") int id, @Query("per_page") int count);

@POST("/event_view/add")
public Call<ApiResult<UserEventDetail>> addEventView(@Body UserEventDetail userEventDetail);

/*------------------------------------------------------ file upload/ download ---------------------------------------------------*/
@Multipart
@POST("uploadapp")
Call<ResponseBody> uploadAvatar(@Query("user_id") int user_id, @Part("description") String description, @Part("userfile\"; filename=\"avatar.png\"") RequestBody image);

@GET("avatar")
@Streaming
public Call<ResponseBody> getAvatarUrl(@Query("user_id") int user_id);

}

当我需要实现网络任务时,我会使用这些代码。

Routes api = Api.getInstance().createService(Routes.class);
Call<ApiResult<User>> call = api.createUser(user);
Response response = call.execute();

将所有API集成到Routes类中有什么不好吗?

1 个答案:

答案 0 :(得分:0)

这没有任何错误。实际上我会说这是一个很好的做法,你将所有API列入Route类,并且使用是很常见的。