2015-08-31 178 views
0

我刚开始使用Volley的库进行http调用,并且我尝试使用RequestFuture类进行同步请求,但是我无法做出简单请求。任何想法我做错了什么?Volley同步请求

RequestQueue requestQueue = Volley.newRequestQueue(this); 
    String url = "http://myapi-oh.fr/v2/podcasts/x/shows/" + points.get(0).getShowId() + "/streams"; 

    RequestFuture<JSONObject> future = RequestFuture.newFuture(); 
    JsonObjectRequest request = new JsonObjectRequest(url, null, future, future); 
    requestQueue.add(request); 


    try { 
     JSONObject response = future.get(10, TimeUnit.SECONDS);; // this will block (forever) 
     points.get(0).setStreamUrl(response.getJSONArray("result").getJSONObject(0).getString("url")); 
    } catch (InterruptedException e) { 
     Log.d(TAG, "error : " + e); 
     // exception handling 
     Log.d(TAG, "error : " + e); 
    } catch (ExecutionException e) { 
     // exception handling 
     Log.d(TAG, "error : " + e); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Log.d(TAG, "error : " + e); 
    } catch (TimeoutException e) { 
     e.printStackTrace(); 
    } 

回答

-1

要做出简单的请求,只需使用StringRequest类。下面是一个小片段:

// Instantiate the RequestQueue. 
RequestQueue queue = Volley.newRequestQueue(this); 

StringRequest req = new StringRequest(Request.Method.POST, "your_url", new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       //handle response here. 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       error.printStackTrace(); 
       //handle error here. 
      } 
     }){ 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       //return params(if any) to server here. 
       return super.getParams(); 
      } 
     }; 
queue.add(req); 
+1

是的,我已经在其他情况下做了这些,但是这次我想让它同步! – krakig