<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文:[http://developer.android.com/training/volley/simple.html](http://developer.android.com/training/volley/simple.html) 使用Volley的方式是,你通過創建一個`RequestQueue`并傳遞`Request`對象給它。RequestQueue管理用來執行網絡操作的工作線程,從Cache中讀寫數據,并解析Http的響應內容。`Requests`執行raw responses的解析,Volley會把響應的數據分發給主線程。 這節課會介紹如何使用`Volley.newRequestQueue`這個建立請求隊列RequestQueue的方法來發送一個請求,在下一節課[建立一個請求隊列Setting Up a RequestQueue](#)中會介紹你自己如何建立一個請求隊列。 這節課也會介紹如何添加一個請求到RequesutQueue以及如何取消一個請求。 ### 1)Add the INTERNET Permission 為了使用Volley,你必須添加`android.permission.INTERNET`權限到你的manifest文件中。沒有這個權限,你的app將無法訪問網絡。 ### 2)Use newRequestQueue Volley提供了一個簡便的方法:`Volley.newRequestQueue`用來為你建立一個`RequestQueue`,使用默認值,并啟動這個隊列。例如: ~~~ final TextView mTextView = (TextView) findViewById(R.id.text); ... // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url ="http://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener() { @Override public void onResponse(String response) { // Display the first 500 characters of the response string. mTextView.setText("Response is: "+ response.substring(0,500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mTextView.setText("That didn't work!"); } }); // Add the request to the RequestQueue. queue.add(stringRequest); ~~~ Volley總是將解析后的數據返回至主線程中。在主線程中更加合適使用接收到的數據用來操作UI控件,這樣你可以在響應的handler中輕松的修改UI,但是對于庫提供的一些其他方法是有些特殊的,例如與取消有關的。 關于如何創建你自己的請求隊列,而不是使用Volley.newRequestQueue方法,請查看[建立一個請求隊列Setting Up a RequestQueue](#)。 ### 3)Send a Request 為了發送一個請求,你只需要構造一個請求并通過`add()`方法添加到`RequestQueue`中。一旦你添加了這個請求,它會通過隊列,得到處理,然后得到原始的響應數據并返回。 當你執行`add()`方法時,Volley觸發執行一個緩存處理線程以及一系列網絡處理線程。當你添加一個請求到隊列中,它將被緩存線程所捕獲并觸發:如果這個請求可以被緩存處理,那么會在緩存線程中執行響應數據的解析并返回到主線程。如果請求不能被緩存所處理,它會被放到網絡隊列中。網絡線程池中的第一個可用的網絡線程會從隊列中獲取到這個請求并執行HTTP操作,解析工作線程的響應數據,把數據寫到緩存中并把解析之后的數據返回到主線程。 請注意那些比較耗時的操作,例如I/O與解析parsing/decoding都是執行在工作線程。**你可以在任何線程中添加一個請求,但是響應結果都是返回到主線程的。** 下圖1,演示了一個請求的生命周期: ![volley-request](https://box.kancloud.cn/2015-07-28_55b72471bf719.png) ### 4)Cancel a Request 對請求Request對象調用`cancel()`方法取消一個請求。一旦取消,Volley會確保你的響應Handler不會被執行。這意味著在實際操作中你可以在[activity](# "An activity represents a single screen with a user interface.")的`onStop()`方法中取消所有pending在隊列中的請求。你不需要通過檢測`getActivity() == null`來丟棄你的響應handler,其他類似`onSaveInstanceState()`等保護性的方法里面也都不需要檢測。 為了利用這種優勢,你應該跟蹤所有已經發送的請求,以便在需要的時候可以取消他們。**有一個簡便的方法**:你可以為每一個請求對象都綁定一個tag對象。然后你可以使用這個tag來提供取消的范圍。例如,你可以為你的所有請求都綁定到執行的[Activity](# "An activity represents a single screen with a user interface.")上,然后你可以在`onStop()`方法執行`requestQueue.cancelAll(this)` 。同樣的,你可以為ViewPager中的所有請求縮略圖Request對象分別打上對應Tab的tag。并在滑動時取消這些請求,用來確保新生成的tab不會被前面tab的請求任務所卡到。 下面一個使用String來打Tag的例子: 1. 定義你的tag并添加到你的請求任務中。 ~~~ public static final String TAG = "MyTag"; StringRequest stringRequest; // Assume this exists. RequestQueue mRequestQueue; // Assume this exists. // Set the tag on the request. stringRequest.setTag(TAG); // Add the request to the RequestQueue. mRequestQueue.add(stringRequest); ~~~ 1. 在[activity](# "An activity represents a single screen with a user interface.")的onStop()方法里面,取消所有的包含這個tag的請求任務。 ~~~ @Override protected void onStop () { super.onStop(); if (mRequestQueue != null) { mRequestQueue.cancelAll(TAG); } } ~~~ 當取消請求時請注意:如果你依賴你的響應handler來標記狀態或者觸發另外一個進程,你需要對此進行考慮。再說一次,response handler是不會被執行的。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看