改造 - 最佳编码方法

时间:2018-01-18 11:31:45

标签: android optimization retrofit2

我在我的Android项目中集成了Retrofit。我已成功提出请求并收到了回复。但是,我有两个问题。在我写下问题之前,以下是我的代码:

改造API接口:

public interface GetSensorData {

    @GET("data/normal/{station}/{type}/{period}/{duration}")
    Call<SensorData> getSensorDetails(
        @Path("station") String station,
        @Path("type") String type,
        @Path("period") String period,
        @Path("duration") String duration
    );

    @GET("user/stations")
    Call<List<Station>> getStationList();

    Gson gson = new GsonBuilder()
        .setLenient()
        .create();

    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request original = chain.request();
        Request.Builder requestBuilder = original.newBuilder()
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", "Bearer " + Common.TOKEN_ACCESS) 
                .method(original.method(), original.body());

        Request request = requestBuilder.build();
        return chain.proceed(request);
        }
    }).build();


    public static final Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
}

以及对服务的异步调用:

public class RetroCall {

final GetSensorData gitHubService = GetSensorData.retrofit.create(GetSensorData.class);
private SensorData contributors = new SensorData();
private ArrayList<ContributorsListener> listeners = new ArrayList<>();

public void requestSensorData() {

    final Call<SensorData> call = gitHubService.getSensorDetails("00000146", "hourly", "last", "5");
    call.enqueue(new Callback<SensorData>() {
        @Override
        public void onResponse(Call<SensorData> call, Response<SensorData> response) {
            if (response.isSuccessful()) {
                contributors = response.body();
                Log.e("Response Body:", response.raw().toString());
            } else {
                for (ContributorsListener listener : listeners) {
                    listener.onContributorsLoadFailed(response.message());
                    System.out.println(response.message());
                }
            }
        }

        @Override
        public void onFailure(Call<SensorData> call, Throwable t) {
            System.out.println(t.getMessage());
            for (ContributorsListener listener : listeners) {
                listener.onContributorsLoadFailed(t.getMessage());
            }
        }
    });
}

public interface ContributorsListener {

    void onContributorsLoaded(List<SensorData> contributors);
    void onContributorsLoadFailed(String message);
    void onContributorLoaded(SensorData contributor);
}
}

问题: 在我的界面中,我已经为我的API定义了两个接口,但这不是结束。我有更多的服务器调用。所以,我不想针对每个接口编写异步调用并检索数据。我想以最佳方式编写它,以便我只编写一个异步调用,它可以处理所有内容。所以,有人可以告诉我如何使用Retrofit做到这一点?

0 个答案:

没有答案