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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文:[http://developer.android.com/training/volley/request.html](http://developer.android.com/training/volley/request.html) 這一課會介紹如何使用Volley支持的常用請求類型: - `StringRequest`。指定一個URL并在相應回調中接受一個原始的raw string數據。請參考前一課的示例。 - `ImageRequest`。指定一個URL并在相應回調中接受一個image。 - `JsonObjectRequest`與`JsonArrayRequest` (均為`JsonRequest`的子類)。指定一個URL并在相應回調中獲取到一個JSON對象或者JSON數組。 如果你需要的是上面演示的請求類型,那么你應該不需要自己實現一個自定義的請求。這節課會演示如何使用那些標準的請求類型。關于如何實現自定義的請求,請看下一課:[實現自定義的請求](#)。 ### 1)Request an Image Volley為請求圖片提供了如下的類。這些類依次有著依賴關系,用來支持在不同的層級進行圖片處理: - `ImageRequest` - 一個封裝好的,用來處理URL請求圖片并且返回一張decode好的bitmap的類。它同樣提供了一些簡便的接口方法,例如指定一個大小進行重新裁剪。它的主要好處是Volley會確保類似decode,resize等耗時的操作執行在工作線程中。 - `ImageLoader` - 一個用來處理加載與緩存從網絡上獲取到的圖片的幫助類。ImageLoader是管理協調大量的ImageRequest的類。例如,在ListView中需要顯示大量縮略圖的時候。ImageLoader為通常的Volley cache提供了更加前瞻的內存緩存,這個緩存對于防止圖片抖動非常有用。。這還使得能夠在避免阻擋或者延遲主線程的前提下在緩存中能夠被Hit到。ImageLoader還能夠實現響應聯合Coalescing,每一個響應回調里面都可以設置bitmap到view上面。聯合Coalescing使得能夠同時提交多個響應,這提升了性能。 - `NetworkImageView` - 在ImageLoader的基礎上建立,替換ImageView進行使用。對于需要對ImageView設置網絡圖片的情況下使用很有效。NetworkImageView同樣可以在view被detached的時候取消pending的請求。 ### 1.1)Use ImageRequest 下面是一個使用ImageRequest的示例。它會獲取指定URL的image并顯示到app上。里面演示的RequestQueue是通過上一課提到的單例類實現的。 ~~~ ImageView mImageView; String url = "http://i.imgur.com/7spzG.png"; mImageView = (ImageView) findViewById(R.id.myImage); ... // Retrieves an image specified by the URL, displays it in the UI. ImageRequest request = new ImageRequest(url, new Response.Listener() { @Override public void onResponse(Bitmap bitmap) { mImageView.setImageBitmap(bitmap); } }, 0, 0, null, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) { mImageView.setImageResource(R.drawable.image_load_error); } }); // Access the RequestQueue through your singleton class. MySingleton.getInstance(this).addToRequestQueue(request); ~~~ ### 1.2)Use ImageLoader and NetworkImageView 你可以使用ImageLoader與NetworkImageView用來處理類似ListView等大量顯示圖片的情況。在你的layout XML文件中,你可以使用NetworkImageView來替代通常的ImageView, 例如: ~~~ <com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="150dp" android:layout_height="170dp" android:layout_centerHorizontal="true" /> ~~~ 你可以使用ImageLoader來顯示一張圖片,例如: ~~~ ImageLoader mImageLoader; ImageView mImageView; // The URL for the image that is being loaded. private static final String IMAGE_URL = "http://developer.android.com/images/training/system-ui.png"; ... mImageView = (ImageView) findViewById(R.id.regularImageView); // Get the ImageLoader through your singleton class. mImageLoader = MySingleton.getInstance(this).getImageLoader(); mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView, R.drawable.def_image, R.drawable.err_image)); ~~~ 然而,如果你要做得是為ImageView進行圖片設置,你可以使用NetworkImageView來實現,例如: ~~~ ImageLoader mImageLoader; NetworkImageView mNetworkImageView; private static final String IMAGE_URL = "http://developer.android.com/images/training/system-ui.png"; ... // Get the NetworkImageView that will display the image. mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView); // Get the ImageLoader through your singleton class. mImageLoader = MySingleton.getInstance(this).getImageLoader(); // Set the URL of the image that should be loaded into this view, and // specify the ImageLoader that will be used to make the request. mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader); ~~~ 上面的代碼是通過前一節課的單例模式來實現訪問到RequestQueue與ImageLoader的。之所以這樣做得原因是:對于ImageLoader(一個用來處理加載與緩存圖片的幫助類)來說,單例模式可以避免旋轉所帶來的抖動。使用單例模式可以使得bitmap的緩存與[activity](# "An activity represents a single screen with a user interface.")的生命周期無關。如果你在[activity](# "An activity represents a single screen with a user interface.")中創建ImageLoader,這個ImageLoader有可能會在手機進行旋轉的時候被重新創建。這可能會導致抖動。 ### 1.3)Example LRU cache Volley工具箱中提供了通過DiskBasedCache實現的一種標準緩存。這個類能夠緩存文件到磁盤的制定目錄。但是為了使用ImageLoader,你應該提供一個自定義的內存LRC緩存,這個緩存需要實現`ImageLoader.ImageCache`的接口。你可能想把你的緩存設置成一個單例。關于更多的有關內容,請參考[建立請求隊列Setting Up a RequestQueue](#). 下面是一個內存LRU Cache的實例。它繼承自LruCache并實現了ImageLoader.ImageCache的接口: ~~~ import android.graphics.Bitmap; import android.support.v4.util.LruCache; import android.util.DisplayMetrics; import com.android.volley.toolbox.ImageLoader.ImageCache; public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache { public LruBitmapCache(int maxSize) { super(maxSize); } public LruBitmapCache(Context ctx) { this(getCacheSize(ctx)); } @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } @Override public Bitmap getBitmap(String url) { return get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { put(url, bitmap); } // Returns a cache size equal to approximately three screens worth of images. public static int getCacheSize(Context ctx) { final DisplayMetrics displayMetrics = ctx.getResources(). getDisplayMetrics(); final int screenWidth = displayMetrics.widthPixels; final int screenHeight = displayMetrics.heightPixels; // 4 bytes per pixel final int screenBytes = screenWidth * screenHeight * 4; return screenBytes * 3; } } ~~~ 下面是如何初始化ImageLoader并使用cache的實例: ~~~ RequestQueue mRequestQueue; // assume this exists. ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(LruBitmapCache.getCacheSize())); ~~~ ### 2)Request JSON Volley提供了以下的類用來執行JSON請求: - `JsonArrayRequest` - 一個為了獲取JSONArray返回數據的請求。 - `JsonObjectRequest` - 一個為了獲取JSONObject返回數據的請求。允許把一個JSONObject作為請求參數。 這兩個類都是繼承自JsonRequest的。你可以使用類似的方法來處理這兩種類型的請求。如下演示了如果獲取一個JSON feed并顯示到UI上: ~~~ TextView mTxtDisplay; ImageView mImageView; mTxtDisplay = (TextView) findViewById(R.id.txtDisplay); String url = "http://my-json-feed"; JsonObjectRequest jsObjRequest = new JsonObjectRequest (Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) { mTxtDisplay.setText("Response: " + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub } }); // Access the RequestQueue through your singleton class. MySingleton.getInstance(this).addToRequestQueue(jsObjRequest); ~~~ 關于基于[Gson](http://code.google.com/p/google-gson/)實現一個自定義的JSON請求對象,請參考下一節課:[實現一個自定義的請求Implementing a Custom Request](#).
                  <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>

                              哎呀哎呀视频在线观看