当前位置: 首页 > 面试题库 >

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

卞云瀚
2023-03-14
问题内容

我正在使用凌空特技从REST
api解析电影详细信息,并将解析后的数据保存在一个名为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 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 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);
}


 类似资料:
  • 所以我想打印响应的结果,然后使用gson为模型生成数据,但响应永远不会得到返回,因为调用了onResponse。! 如果你注意到Logcat,日志。我的回答没有显示。。!?但是在使用该请求的活动的onSuccess中,它通常会显示日志,但是如果日志包含响应对象,它不会显示哪个非常奇怪,也没有任何意义。。!? Logcat公司 我尝试使用具有相同url和参数的PostMan测试请求,它通常返回jso

  • 我使用的是volley.jar,在编译时没有错误,但是有运行时异常。 然后添加volley.jar和build path。什么是问题? CustomAdapter.java

  • 我有一个凌空的JSONRequest,它被卡住了,因为来自服务器的响应不是有效的JSON(它是html)。 如何才能优雅地处理此错误?我有以下实现: 然而,当我发送请求并得到无效的JSON响应时,不会调用onErrorResponse。相反,我得到了一个例外 整个应用程序崩溃了。 我设法将该错误跟踪到JsonObjectRequest.java类中的以下行: 调用的行是:返回响应。错误(new P

  • 我正在从服务器下载文件。我用排球来做。 这是我的代码 以下是错误侦听器的错误消息在一些弱网络连接上,我收到以下逻辑消息 我不明白失败的原因是什么,因为大多数错误消息都返回null。我很确定这是因为请求超时,我知道如何增加超时,但是我应该在错误侦听器中编写什么代码,以便在出现错误时获得正确的错误消息。

  • 关于在项目中使用截击,我有几个问题: 这个库可以在任何Java项目中使用,还是仅在Android中使用

  • 我想我只会用jsoup分析数据,用Volley连接/下载?当我使用jsoup.connect(site).timeout(600000)时,我应该用volley来做? 有人能用volley和JSOUP编写/链接一个简单的例子吗?