java–volley-如何从Volley中的OnResponse方法返回ArrayList

java–volley-如何从Volley中的OnResponse方法返回ArrayList,第1张

概述我正在使用volley来解析RESTapi中的MovieDetails,我将解析后的Data保存在名为detailsMovies的对象的Arraylist中.但我无法访问onResponse方法之外的ArrayList.我是Android新手所以我不知道该怎么做.任何帮助表示赞赏!这是代码:`publicclassMovieDetailsActivityextends

我正在使用volley来解析REST API中的MovIE Details,我将解析后的Data保存在名为detailsMovIEs的对象的ArrayList中.但我无法访问onResponse方法之外的ArrayList.我是Android新手所以我不知道该怎么做.
任何帮助表示赞赏!

这是代码:`

    public class MovIEDetailsActivity extends AppCompatActivity {    TextVIEw movIEIDText;    private VolleySingleton volleySingleton;    private RequestQueue mRequestQueue;    private ArrayList<MovIEDetails> detailsMovIEs = new ArrayList<>();//    private String movIETitle ;//    private String movIESummary ;//    private int movIEYear;//    private long movIErating;//    private String movIEYoutubeID;//    private String movIEUrlthumbnail;//    private String movIEDownloadlink720p;//    private String movIEQuality720p;//    private String movIEfileSize720p;//    private String movIEDownloadlink1080p;//    private String movIEQuality1080p;//    private String movIEfileSize1080p;    @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();    }    public voID  parseMovIEDetails(){        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);                    }                } catch (JsONException e) {                    e.printstacktrace();                }            }        }, new Response.ErrorListener() {            @OverrIDe            public voID one rrorResponse(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 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 one rrorResponse(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);}
总结

以上是内存溢出为你收集整理的java – volley-如何从Volley中的OnResponse方法返回ArrayList全部内容,希望文章能够帮你解决java – volley-如何从Volley中的OnResponse方法返回ArrayList所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/web/1113538.html

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

发表评论

登录后才能评论

评论列表(0条)

保存