代码之家  ›  专栏  ›  技术社区  ›  Vikas Biradargoudar

声明的接口为空

  •  0
  • Vikas Biradargoudar  · 技术社区  · 7 年前

    最重要的改造方法。

    下面是代码块,

    接口

    public interface OnResponseListener{
            void getCountryListResponse(ArrayList<String> list,int responseCode);
        }
    

                @Override
                public void onResponse(Call<List<Country>> call, Response<List<Country>> response) {
    
                    Log.d("Response",response.body().toString());
                    for (Country country: response.body())
                    {
                        Log.d("Country",country.toString());
                        Log.d("CommomName",country.getCommonName());
                       // Log.d("BLLL",country.getCommonName());
                       countryList.add(country.getCommonName());
                    }
                    Log.d("Entered","Vikas Entered");
                  //  Log.d("ResonseListener",mResponseListener.toString());//Here throwing error, mResponseListener is null object
                    mResponseListener.getCountryListResponse(countryList,response.code());
                }
    

    接口声明

    private  OnResponseListener mResponseListener;
    

    我不知道为什么它是空的,即使在对象(接口)被声明之后。任何帮助都将不胜感激。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Khemraj Sharma    7 年前

    我有很多建议给你。

    我正在展示我的课程,它将清除所有这些点

    调用webservice的代码就是这样。

    Call<BaseModel> call;
    new RetroService().call(call, new RetroService.OnResponseListenerRetro<BaseModel>() {
        @Override
        public void onSuccess(String tag, BaseModel response) {
            System.out.println(response);
        }
    
        @Override
        public void onError(String tag, RetroService.ErrorType error) {
            Toast.makeText(, "", Toast.LENGTH_SHORT).show();
        }
    });
    

    如果你有 Model2 类型响应。

    Call<Model2> call;
    new RetroService().call(call, new RetroService.OnResponseListenerRetro<Model2>() {
        @Override
        public void onSuccess(String tag, Model2 response) {
            System.out.println(response);
        }
    
        @Override
        public void onError(String tag, RetroService.ErrorType error) {
            Toast.makeText(, "", Toast.LENGTH_SHORT).show();
        }
    });
    

    追溯服务.java

    import android.support.annotation.Nullable;
    import android.util.Log;
    
    import com.arsdigitech.angpau.R;
    import com.arsdigitech.angpau.appClasses.App;
    import com.google.gson.JsonSyntaxException;
    
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    
    
    public class RetroService {
        public static final String TAG = "RetroService***";
    
        public <T> void call(Call<T> call, OnResponseListenerRetro<T> onResponseListener) {
            call.enqueue(new Callback<T>() {
                @Override
                public void onResponse(Call<T> call, Response<T> response) {
                    if (response.isSuccessful()) {
                        onResponseListener.onSuccess("", response.body());
                    } else {
                        ErrorType errorType = ErrorType.getErrorTypeByVolleyError(response.code());
                        onResponseListener.onError("", errorType);
                    }
                }
    
                @Override
                public void onFailure(Call<T> call, Throwable t) {
                    Log.e(TAG, t.getMessage());
                    if (t instanceof JsonSyntaxException) {
                        onResponseListener.onError("", ErrorType.ParseError);
                    } else {
                        onResponseListener.onError("", ErrorType.Error);
                    }
                }
            });
        }
    
    
        public enum ErrorType {
            Error(R.string.error),
            RequestTimeoutError(R.string.timeoutError),
            NoConnectionError(R.string.noConnectionError),
            AuthFailureError(R.string.authFailureError),
            ServerError(R.string.serverError),
            NetworkError(R.string.networkError),
            BadRequestError(R.string.badRequestError),
            ForbiddenError(R.string.forbiddenError),
            NotFoundError(R.string.notFoundError),
            UnsupportedMediaType(R.string.unsupportedMediaType),
            MethodNotAllowedError(R.string.methodNotAllowedError),
            ParseError(R.string.parsing_error),;
            int message;
    
            ErrorType(int message) {
                this.message = message;
            }
    
            public String getMessage() {
                return App.getAppRes().getString(message);
            }
    
            public static @Nullable
            ErrorType getErrorTypeByVolleyError(int errorCode) {
                ErrorType errorType = null;
                switch (errorCode) {
                    case 400:
                        errorType = ErrorType.BadRequestError;
                        break;
                    case 401:
                        errorType = ErrorType.AuthFailureError;
                        break;
                    case 403:
                        errorType = ErrorType.ForbiddenError;
                        break;
                    case 404:
                        errorType = ErrorType.NotFoundError;
                        break;
                    case 408:
                        errorType = ErrorType.RequestTimeoutError;
                        break;
                    case 500:
                    case 501:
                    case 502:
                    case 503:
                    case 504:
                    case 505:
                        errorType = ErrorType.ServerError;
                        break;
                    default:
                        errorType = ErrorType.Error;
                }
                return errorType;
            }
        }
    
        public interface OnResponseListenerRetro<T> {
            void onSuccess(String tag, T response);
            void onError(String tag, ErrorType error);
        }
    
    }
    
        2
  •  0
  •   NehaK    7 年前

    创建方法:

    public void setResponseListener(OnResponseListener mResponseListener){
        this.mResponseListener = mResponseListener;
    }
    

    从你叫这个的地方叫它呼叫.排队()方法如下: :

        RestClientClassObject.setResponseListener(new OnResponseListener() {
            @Override
            void getCountryListResponse(ArrayList<String> list, int responseCode) {
    
                //write code you want to do with list
    
            }
    
        });