简体   繁体   English

如何将Retrofit onResponse方法返回的Json数据发送到其他类?

[英]How to send Json data that is returned by Retrofit onResponse method to other class?

I wanted to refactor code for simplicity and readability and that's why I want to move the code outside the class and return a result to class whenever the method is called. 我想重构代码以简化和可读性,这就是为什么我想在代码外部移动代码并在调用方法时将结果返回给类。

Trying: 试:

 ArrayList<MovieReview> movieReview;

    public ArrayList<MovieReview> getReviews(String id) {
    if (NetworkUtil.isNetworkConnected(getActivity())) {

        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<MovieReviewResponse> call = null;

        call = apiService.getMovieReviews(id, BuildConfig.THE_MOVIE_DB_API_KEY);

        call.enqueue(new Callback<MovieReviewResponse>() {
            @Override
            public void onResponse(Call<MovieReviewResponse> call, Response<MovieReviewResponse> response) {
                movieReview= (ArrayList<MovieReview>) response.body().getMovieReviews();
            }

            @Override
            public void onFailure(Call<MovieReviewResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });

    }
  return movieReview;
}

Output: if I used array list outside the on response gives null value. 输出:如果我在on响应之外使用数组列表给出null值。

but if I called a method from on response and pass the result movieReview, as a parameter, it works fine. 但是如果我从响应中调用一个方法并传递结果movieReview作为参数,它就可以正常工作。

Previously used: 以前用过的:

 public void getReviews(String id) {
    if (NetworkUtil.isNetworkConnected(getActivity())) {

        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<MovieReviewResponse> call = null;

        call = apiService.getMovieReviews(id, BuildConfig.THE_MOVIE_DB_API_KEY);

        call.enqueue(new Callback<MovieReviewResponse>() {
            @Override
            public void onResponse(Call<MovieReviewResponse> call, Response<MovieReviewResponse> response) {
                movieReview = (ArrayList<MovieReview>) response.body().getMovieReviews();
                setData(movieReview);
            }

            @Override
            public void onFailure(Call<MovieReviewResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });

    }

}`

Instead of using only Retrofit make use of RxAndroid . 而不是只使用Retrofit使用RxAndroid By using this you will get response of Observable<T> which consists of three Override methods onCompleted() , onError() and onNext() . 通过使用它,您将得到Observable<T>响应,它包含三个覆盖方法onCompleted()onError()onNext() In onNext() method call your specific activity, pass your data through putExtra and get through getExtra . onNext()方法中调用您的特定活动,通过putExtra传递您的数据并通过getExtra

Output: if I used array list outside the on response gives null value 输出:如果我在on响应之外使用数组列表给出null值

Because that is how asyncronus methods work. 因为这是asyncronus方法的工作方式。 Your return happens before onResponse ever happens, so the list object is null. 您的return发生在onResponse发生之前,因此列表对象为null。

Tip: In ideal situations, you want to always return an empty list, not null anyway. 提示:在理想情况下,您希望始终返回空列表,而不是null


Rename your method. 重命名您的方法。

public ArrayList<MovieReview> getReviews(String id)

To Instead 相反

public ArrayList<MovieReview> getReviews(String id, Callback<MovieReviewResponse> callback)

Replace this code 替换此代码

call.enqueue(new Callback<MovieReviewResponse>() {
    ...
});

With this 有了这个

call.enqueue(callback);

Wherever you call that method 无论你在哪里称呼这种方法

// In Activity
String id = "X";
api.getReviews(id); 

Now do.... 现在做....

// In Activity
String id = "X";
api.getReviews(id, new Callback<MovieReviewResponse>() {
    ...
});

And now from within onResponse , you can update a ListView adapter, or whatever you need to do 而现在从内部 onResponse ,您可以更新一个ListView适配器,或任何你需要做的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM