1

I am trying to make a synchronous Volley networking request. I tried to use request future to do the work. So What I am trying to do is request a JSON array, and parse it to an Array List, then use this list to set up a list view layout.

Here is the Logic code:

public void getFriendList() throws JSONException {
    final String url = "http://ip/users/"+ LoginLogic.hold.getId() +"/friends";
    JSONArray jsonArray = new JSONArray();
    serverRequest.sendToServer(url, jsonArray, "POST");
}

@Override
public void onSuccess(JSONArray arr) throws JSONException {
    for(int i = 0; i < arr.length(); i++){
        JSONObject temp = arr.getJSONObject(i);
        Integer id = temp.getInt("id");
        String email = temp.getString("email");
        String firstName = temp.getString("firstName");
        String lastName = temp.getString("lastName");
        friendList.add(new Friends(id, email, firstName, lastName, "null"));
    }
}

@Override
public void sendToServer(String url, JSONArray newUserArray, String methodType) throws JSONException {
    int method = Request.Method.GET;
    if(methodType.equals("POST")){
        method = Request.Method.POST;
    }
    RequestFuture<JSONArray> future = RequestFuture.newFuture();
    JsonArrayRequest request = new JsonArrayRequest(method, url, newUserArray, future, future);
    AppController.getInstance().addToRequestQueue(request, tag_json_obj);
    try{
        JSONArray response = future.get(5, TimeUnit.SECONDS);
        I.onSuccess(response);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }

I am not really familiar with volley request future, so how can I set up this property? What mistake did I make right here?

0 Answers0