如何为 api 调用制作通用改造库 [英] how to make generic retrofit library for api calling

查看:37
本文介绍了如何为 api 调用制作通用改造库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm working on API integration. i want to make generic class for API integration. which can comfortable with for all API integration.right now i'm using separate code for all API. i'm new in android application development. so please guide me.

 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);
        }
    });

}

推荐答案

这里是调用API的Bast方式

Here is Bast Way to call API

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();
        }
    });
}

在 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")) {

    }

以及对 API 响应的接口调用.

and interface call on API response.

public interface ApiListener {
void success(String strApiName, Object response);
void error(String strApiName, String error);
void failure(String strApiName, String message);
 }

这篇关于如何为 api 调用制作通用改造库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆