1

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.

but if I called a method from on response and pass the result movieReview, as a parameter, it works fine.

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

    }

}`
vicky
  • 412
  • 4
  • 18
  • I guess you're an udacity student and this is Popular movies II project. If you could tell why you are passing data between classes that way or just can you be more specific ? – Gurupad Mamadapur Feb 03 '17 at 16:23
  • 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. – vicky Feb 03 '17 at 17:03

2 Answers2

0

Instead of using only Retrofit make use of RxAndroid. By using this you will get response of Observable<T> which consists of three Override methods onCompleted(), onError() and onNext(). In onNext() method call your specific activity, pass your data through putExtra and get through getExtra.

monnef
  • 3,903
  • 5
  • 30
  • 50
  • You mention "Retrofit". Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide possible links for reference. – OneCricketeer Feb 03 '17 at 16:50
0

Output: if I used array list outside the on response gives null value

Because that is how asyncronus methods work. Your return happens before onResponse ever happens, so the list object is null.

Tip: In ideal situations, you want to always return an empty list, not null anyway.


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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245