OkHttpClient不更新UI变量

时间:2016-03-10 21:42:30

标签: java android

所以这可能是一个简单的问题,但我对android并不太熟悉。我已经使用AsyncTask中内置的orroids工作,但想使用OkHttpClient并尝试其他方法使其更简单。

问题 - 在UI线程运行后,变量mMoviesArray正在更新。我需要它来填充mMoviesArray列表,以便我可以使用该列表中的变量来填充gridview布局。

我是否完全错了,或者我有什么遗失?

    package com.rykuno.movieapp2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Movie movie;
    private List<Movie> mMovieList = new ArrayList<>();
    private static final String LOG_TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getMovieData();
            Log.v(LOG_TAG, mMovieList.size() + "");

    }

    private void getMovieData(){
        String apiKey = "API_KEY_HERE";
        String popMovieUrl = "http://api.themoviedb.org/3/movie/popular?api_key=" + apiKey;

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(popMovieUrl).build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String jsonData = response.body().string();
                        try {
                            mMovieList = Arrays.asList(getMovieDetails(jsonData));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
            }
        });
    }



    private Movie[] getMovieDetails(String jsonData) throws JSONException {
        JSONObject movieDetails = new JSONObject(jsonData);
        JSONArray results = movieDetails.getJSONArray("results");
        Movie[] moviesArray = new Movie[results.length()];
        for(int i = 0; i<results.length(); i++){
            JSONObject movieResult = results.getJSONObject(i);
            movie = new Movie();
            movie.setPoster(movieResult.getString("poster_path"));
            movie.setId(movieResult.getInt("id"));
            movie.setOriginalTitle(movieResult.getString("original_title"));
            movie.setPlot(movieResult.getString("overview"));
            movie.setRating(movieResult.getDouble("vote_average"));
            movie.setReleaseDate(movieResult.getString("release_date"));
            moviesArray[i] = movie;
            Log.v(LOG_TAG, movie.getOriginalTitle());
        }
        return moviesArray;
    }
}

0 个答案:

没有答案