凌空-如何从凌空中的OnResponse方法返回ArrayList

凌空-如何从凌空中的OnResponse方法返回ArrayList,第1张

凌空-如何从凌空中的OnResponse方法返回ArrayList

在您的课程中定义一个接口并实现它。
因此,这里的简单技巧是与回调接口。

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);}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5020809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存