Created
April 28, 2018 10:25
-
-
Save raviyadav4875/54283266ba5c0b9d7a0c936c2bb4f4b4 to your computer and use it in GitHub Desktop.
CallApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void callGetTopRatedMoviesApi() { | |
/** | |
* Checking internet connection before api call. | |
* Very important always take care. | |
*/ | |
if (!Utils.isNetworkAvailable(this)) { | |
Toast.makeText(this, | |
"No internet ..Please connect to internet and start app again", | |
Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
showProgress(); | |
//constructing api url | |
String ws_url = BASE_URL_APPLICATION + MOVIE + TOP_RATED + | |
"?api_key=" + API_KEY + "&language=en-US&page=" + pageNumber; | |
//Using Volley to call api | |
WebApiRequest webApiRequest = new WebApiRequest(Request.Method.GET, | |
ws_url, ReqSuccessListener(), ReqErrorListener()); | |
Volley.newRequestQueue(MovieListingActivity.this).add(webApiRequest); | |
} | |
/** | |
* Success listener to handle the movie listing | |
* process after api returns the movie list | |
* | |
* @return | |
*/ | |
private Response.Listener<String> ReqSuccessListener() { | |
return new Response.Listener<String>() { | |
public void onResponse(String response) { | |
Log.e("movie list_response", response); | |
try { | |
hideProgress(); | |
pageNumber++; | |
MovieList movieListModel = (MovieList) Utils.jsonToPojo(response, MovieList.class); | |
if (movieListModel.getResults() != null && | |
movieListModel.getResults().size() > 0) { | |
moviesRecyclerAdapter.addMovies(movieListModel.getResults()); | |
} else { | |
Log.e(TAG, "list empty=="); | |
} | |
} catch (Exception e) { | |
Log.e(TAG,"Exception=="+e.getLocalizedMessage()); | |
} | |
} | |
}; | |
} | |
/** | |
* To Handle the error | |
* | |
* @return | |
*/ | |
private Response.ErrorListener ReqErrorListener() { | |
return new Response.ErrorListener() { | |
public void onErrorResponse(VolleyError error) { | |
Log.e("volley error", "volley error"); | |
Toast.makeText(MovieListingActivity.this, "" + | |
"Server Error..Please try again after sometime", Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment