如何在android

时间:2017-02-25 18:37:50

标签: android android-volley

嗨我有一个截击请求,我想停止继续运行应用程序从服务器获取完整的json,我想使用本教程Volley - http request in blocking way

但我不明白我必须放在哪里,未来,未来

我必须把volleyRequestQueue.add(请求);

我必须把

放在哪里
 try {
JSONObject response = null;
while (response == null) {
    try {
        response = future.get(30, TimeUnit.SECONDS); // Block thread, waiting for response, timeout after 30 seconds
    } catch (InterruptedException e) {
        // Received interrupt signal, but still don't have response
        // Restore thread's interrupted status to use higher up on the call stack
        Thread.currentThread().interrupt();
        // Continue waiting for response (unless you specifically intend to use the interrupt to cancel your request)
    }
}
// Do something with response, i.e.
new SignUpResponseListener().onResponse(response);
} catch (ExecutionException e) {
// Do something with error, i.e.
new MyErrorListener().onErrorResponse(new VolleyError(e));
} catch (TimeoutException e) {
// Do something with timeout, i.e.
new MyErrorListener().onErrorResponse(new VolleyError(e));
}

我的截击请求是:

  JsonArrayRequest movieReq = new JsonArrayRequest(url,
    new Response.Listener<JSONArray>() {
      @Override
      public void onResponse(JSONArray response) {
      for (int i = 0; i < response.length(); i++) {
      try {
      JSONObject obj = response.getJSONObject(i);
      int lage = (obj.getInt("flage"));

       Toast.makeText(send_new_content.this, "flage "+lage, Toast.LENGTH_SHORT).show();
         } catch (JSONException e) {
             e.printStackTrace();

           }}}
         }, new Response.ErrorListener() {
         @Override
        public void onErrorResponse(VolleyError error) {
         }});
     AppController.getInstance().addToRequestQueue(movieReq);

1 个答案:

答案 0 :(得分:0)

  

我想停止继续运行应用

我知道你想要那个,但不要。这是糟糕的用户体验。

您可以在发生/完成请求时制作new ProgressDialog()并显示/隐藏

您已经在movieReq中传递了响应侦听器,所以也许您应该更专注于将JSON数组解析为某些Java对象并加载适配器以在ListView上显示。