2016-03-29 19 views
1

Android projemde ağ görevi gerçekleştirmek için Retrofit kitaplığını kullandım. Retrofit'i kullanmanın yüksek verimli yolunun ne olduğu konusunda kafa karışıklığım var. Android projesinde Retrofit kütüphanesini kullanmanın yüksek verimli yolu nedir?

Projemde iki sınıfları oluşturulmuş bir API.java ve diğeri Route.java olduğunu.

API.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("https://stackoverflow.com/users/new") 
public Call<ApiResult<User>> createUser(@Body User user); 

@POST("https://stackoverflow.com/users/login") 
public Call<ApiResult<User>> login(@Body User user); 

@POST("https://stackoverflow.com/users/update") 
public Call<ApiResult<User>> update(@Body User user); 

@POST("https://stackoverflow.com/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); 

} 

Ben ağları görevleri başarmak için gereken

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, ben bu kodu kullanın.

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

Tüm API'leri Routes sınıfına entegre ettiğim kötü bir şey var mı?

cevap

0

Hayır, burada yanlış bir şey yok. Aslında bunun, tüm API'larınızı Route sınıfında listelediğiniz ve kullanımın yaygın olduğu iyi bir uygulama olduğunu söyleyeceğim.

İlgili konular