-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity.java
More file actions
72 lines (54 loc) · 2.43 KB
/
MainActivity.java
File metadata and controls
72 lines (54 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.fredjunior.flixster;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.codepath.asynchttpclient.AsyncHttpClient;
import com.codepath.asynchttpclient.callback.JsonHttpResponseHandler;
import com.fredjunior.flixster.adapters.MovieAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Headers;
public class MainActivity extends Activity {
public static final String NOW_PLAYING_URL= "https://api.themoviedb.org/3/movie/now_playing?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed";
public static final String TAG="MainActivity";
List<com.fredjunior.flixster.Models.Movie> movies;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rvMovies= findViewById(R.id.rvmovie);
movies=new ArrayList<>();
// Create the adapter
MovieAdapter movieAdapter = new MovieAdapter(this, movies);
// Set the adapter on the recycler view
rvMovies.setAdapter(movieAdapter);
// Set a Layout Manager on the recycler view
rvMovies.setLayoutManager(new LinearLayoutManager(this));
AsyncHttpClient client = new AsyncHttpClient();
client.get(NOW_PLAYING_URL, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Headers headers, JSON json) {
Log.d(TAG, "OnSuccess");
JSONObject jsonObject = json.jsonObject;
try {
JSONArray results = jsonObject.getJSONArray("results");
Log.i(TAG,"Results:" +results.toString());
movies.addAll(com.fredjunior.flixster.Models.Movie.fromJsonArray(results));
movieAdapter.notifyDataSetChanged();
Log.i(TAG,"Movies:" +movies.size());
} catch (JSONException e) {
Log.e(TAG, "Hit Json exception",e);
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Headers headers, String response, Throwable throwable) {
Log.d(TAG, "OnFailure");
}
});
}
}