2015-08-31 216 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