来自Volley onResponse Android的访问变量

时间:2015-11-04 15:57:34

标签: android jsoup android-volley

我正在向json网址发出请求,并从那里获取信息。基于该信息,我正在为一个url进行html解析的另一个截击请求。我需要返回包含所有数据的数组列表,但我无法在第二个齐射请求中添加数据。这是我的代码

  public ArrayList<List> parseJSONMovies(JSONObject data,View v) throws JSONException {


    JSONObject firstArray;
    JSONArray internalData;
  listMDat= new ArrayList<>();//New Array List
    firstArray=data.getJSONObject("data");
    JSONObject currOb;
    internalData=firstArray.getJSONArray("names");
    currOb=internalData.getJSONObject(0);


  JSONArray films;
    JSONObject cuurFilm;
    JSONObject film;

    films=currOb.getJSONArray("filmographies");
    film=films.getJSONObject(0);
    JSONArray allFilms;
    allFilms=film.getJSONArray("filmography");

    for (int i=0;i<allFilms.length();i++)
    {

        cuurFilm=allFilms.getJSONObject(i);

        getMData=new CLassObject();//Object of the class
        getMData.movie_id=cuurFilm.getString("imdbid");
        getMData.movie_syn=cuurFilm.getString("title");
        //Parse Html by generating the imdb url get the image synopsis and stuff and populate the AlooM Class
        String urlIMD="http://www.imdb.com/title/"+getMData.getMovie_id()+"/";



        RequestQueue requestS = VS.getRequestQueue();

        StringRequest newR = new StringRequest(Request.Method.GET, urlIMD, new Response.Listener<String>() {

            public void onResponse(String response) {
                // Display the first 500 characters of the response string.


                    Document doc = Jsoup.parse(response);
                    Elements image_url = doc.select("link[rel=image_src]");
                  Elements movie_syn=doc.select("meta[property=og:description]");
                    Elements movie_title=doc.select("[property=og:title]");

                   /*I need to add these variables to the classObject*/
                    String s1=image_url.attr("href");
                String s2=movie_syn.attr("content");
                String s3=movie_title.attr("content");
                getMData.movie_title=s3;
                getMData.movie_url=s2;
                getMData,movie_syn=s1;







            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "Machar Jhol !!! " + error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
        requestS.add(newR);

        Toast.makeText(getActivity(),"Step2",Toast.LENGTH_LONG).show();








    }
    Toast.makeText(getActivity(),"Step3",Toast.LENGTH_LONG).show();

    return listMDat;
}

}

1 个答案:

答案 0 :(得分:0)

对于将来可能正在寻找此事的任何人来说,这个问题的答案不是通过访问凌空请求中的变量,而是在异步请求中调用同步请求。我所做的是在Async任务中我调用了一个同步齐射请求,然后在PostExecute上调用了一个函数。这最终起作用了