如何在Android

时间:2018-01-01 08:29:14

标签: android retrofit code-reuse

以下代码是我目前使用Retrofit

的方式
Interface
**********

public interface LeafAPI
{

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
            .addInterceptor(logging)
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ConstantValues.BaseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();

    Retrofit retrofitScr = new Retrofit.Builder()
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(ConstantValues.BaseUrl)
            .build();


    @GET("someController/serviceName")
    Call<ArrayList<UserDetails>> getUserList();
}

Activity中的用法:

private void getUserDetails() 
{
    ServiceAPI api = ServiceAPI.retrofit.create(ServiceAPI.class);
    Call<ArrayList<UserDetails>> call=api.getUserList();
    call.enqueue(new Callback<ArrayList<UserDetails>>() {
        @Override
        public void onResponse(Call<ArrayList<UserDetails>> call, retrofit2.Response<ArrayList<UserDetails>> response) {
            //Process the result and display in the UI  
        }

        @Override
        public void onFailure(Call<ArrayList<UserDetails>> call, Throwable t) {
            //Display Error Message
        }
    });
}

我的情况是,我必须在fetch userlistactivities getUserDetails()。 所以我最终在每个方法中复制了getUserDetails方法。

我想知道是否可以将Class移到单独的retrofit,然后从那里重复使用它。我环顾四周,但require('./File1') require('./File2') 上的所有例子都跟我上面的内容类似。

我真的很感激能帮助我指明正确方向的任何帮助。

2 个答案:

答案 0 :(得分:2)

您可以传递callback本身来处理Activity

public class ApiCalls {
   public static void getUserDetails(Callback<ArrayList<UserDetails>> callback) {
       ServiceAPI api = ServiceAPI.retrofit.create(ServiceAPI.class);
       Call<ArrayList<UserDetails>> call=api.getUserList();
       call.enqueue(callback);
   }
}

Activity上:

ApiCalls.getUserDetails(new Callback<ArrayList<UserDetails>>() {
    @Override
    public void onResponse(Call<ArrayList<UserDetails>> call, retrofit2.Response<ArrayList<UserDetails>> response) {
        //now you can  handle it on your activity, yey.
    }

    @Override
    public void onFailure(Call<ArrayList<UserDetails>> call, Throwable t) {
        //Display Error Message
    }
});

答案 1 :(得分:1)

为此你可以做的是创建一个名为GetListingUtility的类,并将此方法放在此GetListingUtility类中。

代表

public class GetListingUtility{

Context context;
OnItemSelected onItemSelected;

public GetListingUtility(Context context, OnItemSelected onItemSelected) {
    this.context = context;
    this.onItemSelected = onItemSelected;
}

 private void getUserDetails() 
{
    ServiceAPI api = ServiceAPI.retrofit.create(ServiceAPI.class);
    Call<ArrayList<UserDetails>> call=api.getUserList();
    call.enqueue(new Callback<ArrayList<UserDetails>>() {
        @Override
        public void onResponse(Call<ArrayList<UserDetails>> call, retrofit2.Response<ArrayList<UserDetails>> response) {
            //Process the result and display in the UI  
        }

        @Override
        public void onFailure(Call<ArrayList<UserDetails>> call, Throwable t) {
            //Display Error Message
        }
    });
}

}

然后您可以调用此方法,如:

private void callGetCountriesWS() {
    GetListingUtility utility = new GetListingUtility(getActivity(), new OnItemSelected() {
        @Override
        public void onItemSelected(Object kv) {
            // your stuff you want to do
        }
    });
    utility.getUserDetails();
}