Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

android - Volley and AsyncTask

I read a post about Volley and I know it's great networking library. But I couldn't understand one thing.

All requests are Async Task or not?

When I want to send asyncTask request using Volley do I need put Volley request in AsyncTask? or should I just call Volley Request if it is already AsyncTask request?

 private class MyClass extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
           // do Volley request
        }
}

Is this right approach?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to run Volley request on AsyncTask.

Why:

They manage all network related task on separate thread. If you look closely at library project they did not picture the AsyncTask. But they intelligently handle all network related task efficiently.

Check RequestQueue.java class in Volley's main package

here I am pasting java doc.

/**
 * A request dispatch queue with a thread pool of dispatchers.
 *
 * Calling {@link #add(Request)} will enqueue the given Request for dispatch,
 * resolving from either cache or network on a worker thread, and then delivering
 * a parsed response on the main thread.
 */

Edited:

Forming a Request:

With Volley, network communication is managed by the RequestQueue. The best way to utilize the RequestQueue and all of its tools, especially the cache, is by instantiating it once and keeping it around as a singleton. At this point you can then add or cancel requests, stop or start requests, and access the response cache(s).

RequestQueue queue =Volley.newRequestQueue(this);

Once the RequestQueue has been instantiated a request must be formed. This can be done utilizing a few different “out of the box” request classes included with the Volley Library or by extending Volley’s request class into your own custom request. The request classes already included in Volley are a String request, JSON requests, and an Image Request. Most of the request classes included in Volley library utilize constructors much like the one below.

Parameters being passed into constructor:

RequestMethod(get, post, delete, ect) JSONObject-An optional object that will be posted with your request ResponseListener- Where your data will go after the request is complete ErrorListener – What will be told when there was a problem with your request.

JsonObjectRequest request = JsonObjectRequest(Requestmethod, url, null,  new ResponseListener(), new ErrorListener());

Listners to receive response:

Successful Response Listener

private class ResponseListener implements Response.Listener{
  @Override
  public void onResponse(JSONObject response){

  }
}

Error Response Listener

private class ErrorListener implements Response.ErrorListener{
  @Override
  public void onErrorResponse(VolleyError error){

  }
}

Finally add your request to Request queue, rest of everything Volley will handle for you.

Making call:

Now, that we have made our request and response classes we are ready to add the request to the queue and retrieve the data. To do so we simply add the request to the queue.

queue.add(request);

The response or error will then be delivered to the response/error classes that we defined in our request. You can add as many requests to the queue that you would like at one time and the responses will be delivered to their respective response/error classes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...