如何在收到json请求后生成一段代码?

时间:2016-05-19 16:29:50

标签: android android-volley

public void doMySearch(List<String> IDs) {

    for (String Id : IDs) {


        String url = "http://api.tvmaze.com/shows/" + Id + "?embed=nextepisode";

        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println("ONREQUEST ");

                        try {

                            String name = response.getString("name");
                            String airdate = response.getJSONObject("_embedded").getJSONObject("nextepisode").getString("airdate");

                            HashMap<String, String> episodesToDisplay = new HashMap<String, String>();
                            episodesToDisplay.put(name, airdate);
                            listOfEpisodes.add(episodesToDisplay);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub

                    }
                });
        System.out.println("BEFORE ADD TO REQUEST QUEUE ");
        MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
        System.out.println("AFTER ADD TO REQUEST QUEUE ");
    }
    System.out.println("AFTER FOR LOOP ");
}

在代码中查看我的打印件,我在ID列表中获得的4个项目的输出是:

BEFORE ADD TO REQUEST QUEUE
AFTER ADD TO REQUEST QUEUE
BEFORE ADD TO REQUEST QUEUE
AFTER ADD TO REQUEST QUEUE
BEFORE ADD TO REQUEST QUEUE
AFTER ADD TO REQUEST QUEUE
BEFORE ADD TO REQUEST QUEUE
AFTER ADD TO REQUEST QUEUE
AFTER FOR LOOP
ONREQUEST
ONREQUEST
ONREQUEST
ONREQUEST

System.out.println("AFTER FOR LOOP ");onRequest之前执行,我希望它在该地方调用一个函数,该函数将使用填充在onRequest内的列表。我认为onRequest会在将项目添加到队列后执行,但事实并非如此。有什么方法可以让我按照我期望的顺序工作吗?我无法在onRequest内调用函数,因为我希望它使用完全填充的列表,并且onRequest会一次调用多次。

2 个答案:

答案 0 :(得分:1)

你必须创建一个回调。

喜欢这个

public interface VolleyCallback {
    void onSuccess(String string);

    void onFailure(VolleyError error);
}

然后当你调用你的doMySearch函数时,你会传递你的回调,如下所示:

doMySearch(ids, new VolleyCallback() {
  @Override
  public void onSuccess(String response) {
   Log.d("Success", response);
  }

  @Override
  public void onFailure(VolleyError error) {                                                              
   Log.e("Error", "ops thats an error!");
  }
});

最后,在你的doMySearch函数中,你覆盖onResponse和onErrorResponse监听器,你可以调用你的回调,如下所示:

    public void doMySearch(List<String> IDs, final VolleyCallback callback) {
    for (String Id : IDs) {

        String url = "http://api.tvmaze.com/shows/" + Id + "?embed=nextepisode";

        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println("ONREQUEST ");

                        callback.onSuccess("ONREQUEST");

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        callback.onFailure(error);
                    }
                });
        System.out.println("BEFORE ADD TO REQUEST QUEUE ");
        MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
        System.out.println("AFTER ADD TO REQUEST QUEUE ");
    }
    System.out.println("AFTER FOR LOOP ");
}

答案 1 :(得分:0)

您的代码块是异步执行的,因此您必须将代码放在onResponse方法中。