如何制作用于api调用的通用改造库

web*_*ted 2 android retrofit

我正在研究 API 集成。我想为 API 集成创建通用类。它可以适应所有 API 集成。现在我对所有 API 使用单独的代码。我是 Android 应用程序开发的新手。所以请指导我。

 public void getHomeCategoryDetailApi(Context context) {
    final ProgressDialog loadingDialog = ProgressDialog.show(context, "Please wait", "Loading...");
    Retrofit restAdapter = ApiLists.retrofit;
    ApiLists apiCall = restAdapter.create(ApiLists.class);
    Call<HomeCategoryModelClass> call = apiCall.homePageCatListAPI();
    Log.d(TAG, "CategoryDetail : " + call.request()+" \n"+apiCall.homePageCatListAPI().toString());

    call.enqueue(new Callback<HomeCategoryModelClass>() {
        @Override
        public void onResponse(Call<HomeCategoryModelClass> call, Response<HomeCategoryModelClass> response) {
            Log.d(TAG, "onResponse: CategoryDetail:" + response.body());
            Log.d(TAG, "onResponse:  response.code():" + response.code());
            if (response.body() == null) {
                loadingDialog.dismiss();
                globalClass.showAlertDialog(getActivity(), getString(R.string.InternetAlert), getString(R.string.InternetMessage), false);
            } else {
                loadingDialog.dismiss();
                if (response.body().getStatusCode().equalsIgnoreCase("1")) {
                    homeCategoryImageMenu = (ArrayList<Menu>) response.body().getMenu();
                    thirdHorizontalRecyclerAdapter.notifyDataSetChanged();
                } else {
                    globalClass.showAlertDialog(getActivity(), "Alert", "" + response.body().getStatus(), false);
                }
            }
            if (response.errorBody() != null) {
                try {
                    Log.d(TAG, "onResponse: response.errorBody()===>" + response.errorBody().string());
                    if (loadingDialog.isShowing() && loadingDialog != null) {
                        loadingDialog.dismiss();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<HomeCategoryModelClass> result, Throwable t) {
            Log.d(TAG, "onFailure: " + result.toString());
            loadingDialog.dismiss();
            globalClass.showAlertDialog(getActivity(), getString(R.string.InternetAlert), getString(R.string.InternetMessage), false);
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

web*_*ted 6

这是调用API的Bast方式

public class APIResponse {
private static String TAG = APIResponse.class.getSimpleName();

public static <T> void callRetrofit(Call<T> call, final String strApiName, Context context, final ApiListener apiListener) {
    final ProgressDialog progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("Loading...");
    progressDialog.setCancelable(false);
    progressDialog.show();

    call.enqueue(new Callback<T>() {
        @Override
        public void onResponse(Call<T> call, Response<T> response) {
            if (strApiName.equalsIgnoreCase("LoginApi")) {

                if (response.isSuccessful()) {
                    Log.d(TAG, "onResponse: " + response.body().toString());
                    //  NearByNurse nearByNurse = (NearByNurse) response.body(); // use the user object for the other fields
                    // apiListener.success(url,nearByNurse);
                    progressDialog.dismiss();
                } else {
                    try {
                        Log.d(TAG, "onResponse: " + response.errorBody().string());
                        apiListener.error(strApiName, response.errorBody().string());
                        progressDialog.dismiss();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else if (strApiName.equalsIgnoreCase("")) {
                //Patient user = (Patient) response.body();
            }
        }

        @Override
        public void onFailure(Call<T> call, Throwable t) {
            Log.d(TAG, "onFailure: " + t.toString());
            if (strApiName.equalsIgnoreCase("searchNearbyTest")) {
                apiListener.failure(strApiName, t.toString());
            }
            progressDialog.dismiss();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在API调用端

private void loginApi() {
    Retrofit retrofit = ApiLists.retrofit;
    ApiLists apiList = retrofit.create(ApiLists.class);
    Call<JsonElement> loginApiCall = apiList.loginApi("kjdf", "fkldngdkl", "lkfdxngl", "kjngn", "jksdgkj");
    APIResponse.callRetrofit(loginApiCall, "LoginApi", LoginActivity.this, this);
}
@Override
public void success(String strApiName, Object response) {
    if (strApiName.equals("LoginApi")) {
    }
}

@Override
public void error(String strApiName, String error) {
    if (strApiName.equals("LoginApi")) {

    }
}

@Override
public void failure(String strApiName, String message) {
    if (strApiName.equals("LoginApi")) {

    }
Run Code Online (Sandbox Code Playgroud)

以及API响应上的接口调用。

public interface ApiListener {
void success(String strApiName, Object response);
void error(String strApiName, String error);
void failure(String strApiName, String message);
 }
Run Code Online (Sandbox Code Playgroud)