凌空的AsyncTaskAsyncTask

2023-09-13 00:08:56 作者:孤存

我读了关于乱射后,我知道这是伟大的网络库。但我不明白的一件事。

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

所有的请求是异步任务或不?

All request is Async Task or not?

当我想用排枪,我需要把排球请求的AsyncTask发送AsyncTask的请求? 或者我应该只是调用凌空要求它已经AsyncTask的要求吗?

when I want send asyncTask request using Volley do I need put Volley request in AsyncTask? or should I just call Volley Request it's already AsyncTask request?

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

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

这是正确的做法?

Is this right approach?

推荐答案

您不必运行异步任务Volly的请求。

You don't need to run Volly request on async task.

原因:

他们管理上的独立线程中的所有网络相关的任务。 如果你仔细观察,在库项目中,他们并没有想象的异步任务。 但他们聪明地有效地处理所有与网络相关的任务。

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

检查 RequestQueue.java 在Volly的主包类

Check RequestQueue.java class in Volly's main package

我在这里贴的Java文档。

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.
 */

编辑:

形成一个请求

随着排球,网络通讯被请求队列管理。利用该请求队列和所有的工具,尤其是在高速缓存中,最好的办法是通过一次实例,并围绕保持它作为一个单身。在这一点上,你可以再加入或取消的请求,停止或启动请求,并访问响应缓存(S)。

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);

一旦请求队列被实例化的要求,必须形成。这可以通过使用几个不同的开箱即用请求类包含的凌空库或通过扩展排球的请求类到自己的自定义请求。已列入抽射请求类是一个字符串请求,JSON的请求,以及图像请求。大部分包含在排球库中的请求类的利用构造很像下面的人。

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.

参数被传递到构造函数:

RequestMethod(GET,POST,删除等) * 的JSONObject 的* - 将张贴你的要求的可选对象 ResponseListener - 如果你的数据将在请求后去完成 ErrorListener - 什么会被告知当有您的要求问题

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接收响应:

成功响应监听器

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

  }
}

错误响应监听器

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

  }
}

Finaly添加您的要求请求队列,everithing volly会为您处理的其余部分。

Finaly add your request to Request queue, rest of everithing volly will handle for you.

拨打电话:

现在,我们已经取得了我们的请求和响应类,我们准备添加请求队列和检索数据。要做到这一点,我们只需将请求队列中。

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

 
精彩推荐
图片推荐