AsyncTask不返回任何值

时间:2017-06-20 22:27:21

标签: java android json android-asynctask

我有一个适配器类和一个工作正常的模型类。我相信内部AsyncTask有问题,我无法将json的结果显示在主要活动上。请帮忙。

我还包括了json响应的样子。

public class MainActivity extends AppCompatActivity {

public ArrayList<NetworkUtils> moviesList = new ArrayList<>();
private RecyclerView mRecyclerView;
private MoviesAdapter mMoviesAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.rv_main_activity);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));

    mMoviesAdapter = new MoviesAdapter(this);

    mRecyclerView.setAdapter(mMoviesAdapter);
    mMoviesAdapter.setMoviesList(moviesList);

    new FetchMoviesTask().execute("my url goes here");
}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class FetchMoviesTask extends AsyncTask<String, Void, List<NetworkUtils>> {

    @Override
    public List<NetworkUtils> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        String json;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            reader = new BufferedReader(new InputStreamReader(stream));


            json = buffer.toString(); 

            JSONObject parentObject = new JSONObject(json); 
            JSONArray parentArray = parentObject.getJSONArray("results");


            for (int i = 0; i < parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i); 
                NetworkUtils tmdbMovies = new NetworkUtils();
                tmdbMovies.setTitle(finalObject.getString("title"));

                moviesList.add(tmdbMovies);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return moviesList;
    }

    @Override
    public void onPostExecute(List<NetworkUtils> moviesList) {
        super.onPostExecute(moviesList);
    }
}}

以下是json的回复:

    {
    "results": [{
    "title": "Wonder Woman",
}, {

    "title": "Beauty and the Beast",
}, {

    "title": "Logan"
}

适配器类

public class MoviesAdapter extends 
RecyclerView.Adapter<MoviesAdapter.MoviesViewHolder> {

private List<NetworkUtils> moviesList = new ArrayList<>();
private Context context;

public class MoviesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView titleTextView;

    public MoviesViewHolder(View itemView) {
        super(itemView);
        titleTextView = (TextView) itemView.findViewById(R.id.movie_title);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View itemView) {
        Context context = itemView.getContext();
        Intent showMovieIntent = new Intent(context, MainActivity.class); 
        context.startActivity(showMovieIntent);
    }
}
public MoviesAdapter(List<NetworkUtils> moviesList) {
    this.moviesList = moviesList;
}
public MoviesAdapter(MainActivity mainActivity) {
}

@Override
public MoviesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.image_layout, parent, false);
    return new MoviesViewHolder(view);
}

@Override
public void onBindViewHolder(MoviesViewHolder holder, int position) {
    NetworkUtils movies = moviesList.get(position);
    TextView mTextView = holder.titleTextView;
    mTextView.setText(movies.getTitle());
}

@Override
public int getItemCount() {
    if (moviesList == null)
        return 0;
        return moviesList.size();
}
public void setMoviesList(ArrayList<NetworkUtils> mMoviesList) {
    moviesList = mMoviesList;
    notifyDataSetChanged();
}
}

0 个答案:

没有答案