在您的课程中定义一个接口并实现它。
因此,这里的简单技巧是与回调接口。
public class MovieDetailsActivity extends AppCompatActivity { TextView movieIdText;private VolleySingleton volleySingleton;private RequestQueue mRequestQueue;private ArrayList<MovieDetails> detailsMovies = new ArrayList<>();@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_movie_details); Intent i = getIntent(); String movieId = i.getExtras().getString("movieId"); volleySingleton = VolleySingleton.getsInstance(); mRequestQueue = VolleySingleton.getmRequestQueue(); parseMovieDetails(new CallBack() { @Override public void onSuccess(ArrayList<MovieDetails> detailsMovies) { // Do Stuff } @Override public void onFail(String msg) { // Do Stuff } });}public void parseMovieDetails(final CallBack onCallBack){ JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, getUrl(100), new Response.Listener<JSONObject>() { @Override public void onResponse(JSonObject response) { MovieDetails movieDetails = new MovieDetails(); if (response == null || response.length() == 0) { Toast.makeText(getApplicationContext(), "Null Object", Toast.LENGTH_LONG).show(); } try { JSonObject movieData = response.getJSonObject("data"); JSonObject movieDetailsObject = movieData.getJSonObject(Keys.EndPointMovieDetails.KEYS_MOVIE); String movieTitle = movieDetailsObject.getString(Keys.EndPointMovieDetails.KEYS_TITLE_LONG); long movieRating = movieDetailsObject.getInt(Keys.EndPointMovieDetails.KEYS_RATING); String movieSynopsis = movieDetailsObject.getString(Keys.EndPointMovieDetails.KEYS_DEscriptION); String moviePosterUrl = movieDetailsObject.getString(Keys.EndPointMovieDetails.KEYS_COVER); String movieYoutubeId = movieDetailsObject.getString(Keys.EndPointMovieDetails.KEY_YOUTUBE_ID); movieDetails.setTitle(movieTitle); movieDetails.setRating(movieRating); movieDetails.setSummary(movieSynopsis); movieDetails.setUrlThumbnail(moviePosterUrl); movieDetails.setYoutube_id(movieYoutubeId); detailsMovies.add(movieDetails); JSonArray torrentDownloadlinks = movieDetailsObject.getJSonArray(Keys.EndPointMovieDetails.KEYS_TORRENTS); for(int i=0;i<torrentDownloadlinks.length();i++) { JSonObject urlInfo = torrentDownloadlinks.getJSonObject(i); String urlTorrent = urlInfo.getString("url"); String quality = urlInfo.getString("quality"); String fileSize = urlInfo.getString("size"); movieDetails.setDownloadlink(urlTorrent); movieDetails.setQuality(quality); movieDetails.setFileSize(fileSize); detailsMovies.add(movieDetails); } onCallBack.success(detailsMovies); } catch (JSonException e) { e.printStackTrace(); onCallBack.onFail(e.toString()); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); mRequestQueue.add(jsonObjectRequest);}public String getUrl(int movieId){ return UrlEndPoints.URL_MOVIE_DETAILS+ UrlEndPoints.URl_CHAR_QUESTION+ UrlEndPoints.URL_PARAM_ID+movieId;} public interface CallBack { void onSuccess(ArrayList<MovieDetails> detailsMovies); void onFail(String msg);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)