Warle
Warle

Reputation: 123

Changing Volley call to AsyncTask

I would like to change this function to be able to wait until is done, cause when I use it to log in via API, it takes some time to get answer and my app now says login failed cause it doesn't wait for answer.

I know I need to change my class to extends AsyncTask, but what I don't know ho to change this function to doInBackground(), because in every tutorial they send to doInBackground only URLs and returns string, but I need to send to function also request type, body and callbackID.

private void createCall(int type, String url, JSONObject data, final int callback) {
    JsonObjectRequest jsonRequest = new JsonObjectRequest(type, url,data,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Response", response.toString());
                    try {
                        callback(response, callback);
                    } catch (Exception e){
                        Log.d("API callback error", e.getMessage());
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error response", error.toString());
                }
            }
    );
    queue.add(jsonRequest);
}

I would like to be able to wait until I get any result from api call.

Upvotes: 0

Views: 105

Answers (2)

Kilarn123
Kilarn123

Reputation: 733

You can use RequestFuture (examples). The advantage is that you don't have to mess with callbacks when you have to wait for an answer.

//show dialog to let the user know something is happening
ProgressDialog progress = new ProgressDialog(getActivity());
progress.setMessage("Fetching The File....");
progress.show();

//create request
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(type, url, data, future, future);
RequestQueue requestQueue = Volley.newRequestQueue(c);
requestQueue.add(request);

JSONObject response = null;

try{
    //this will wait for response
    response = future.get();
    //response arrived, you can dismiss the dialog
    progress.dismiss()

    /*
     * do things with response here
     */
}catch(ExecutionException e){
    if(e.getCause() instanceof VolleyError){
        VolleyError ve = (VolleyError)e.getCause();
        //handle volley error (error code etc)
    }else{
        e.printStackTrace();
        //handle other exception
    }
}

Upvotes: 0

Sultan Mahmud
Sultan Mahmud

Reputation: 1285

If you want to freeze UI thread until response back then you can use progress dialog and don't need to use asynctask . Start the progress dialog once you add the request object in the queue.

//add the request to the queue
queue.add(jsonRequest); 

//initialize the progress dialog and show it
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Fetching The File....");
progressDialog.show();

Then dismiss the dialog once you have received the response from the server.

@Override
    public void onResponse(String response) {
//after finishing all work
        progressDialog.dismiss();
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(“Volly Error”,”Error: ”+error.getLocalizedMessage());
        progressDialog.dismiss();
    }
});

Upvotes: 2

Related Questions