Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解
Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解
首先上效果圖
Logcat日志信息on Reponse
Volley特別適合數(shù)據(jù)量不大但是通信頻繁的場(chǎng)景,像文件上傳下載不適合!
首先第一步
用到的RequetQueue
RequestQueue.Java
RequestQueue請(qǐng)求隊(duì)列首先得先說(shuō)一下,ReuqestQueue是如何對(duì)請(qǐng)求進(jìn)行管理的...RequestQueue是對(duì)所有的請(qǐng)求進(jìn)行保存...然后通過(guò)自身的start()方法開(kāi)啟一個(gè)CacheDispatcher線(xiàn)程用于緩存調(diào)度,開(kāi)啟若干個(gè)NetWorkDispatcher線(xiàn)程用于網(wǎng)絡(luò)調(diào)度...那么為什么要這樣設(shè)計(jì)呢?
因?yàn)橐粋€(gè)請(qǐng)求如果已經(jīng)提交了一次,那么就只需要處理這一次結(jié)果就可以了,對(duì)于多次重復(fù)的請(qǐng)求,我們完全可以使用一個(gè)緩存來(lái)進(jìn)行保存..從而減少一些不必要的網(wǎng)絡(luò)請(qǐng)求,減小服務(wù)器的負(fù)擔(dān)...如果一個(gè)請(qǐng)求在緩存中沒(méi)存在過(guò),那么我們?cè)賵?zhí)行網(wǎng)絡(luò)請(qǐng)求就可以了
總而言之,設(shè)計(jì)理念就是從RequestQueue取出請(qǐng)求,先判斷緩存是否命中,如果緩存命中,則從緩存中取出數(shù)據(jù),如果緩存沒(méi)有命中,則提交網(wǎng)絡(luò)請(qǐng)求,從服務(wù)器端獲取數(shù)據(jù)
導(dǎo)入volley.jar 到您的Project libs里面,然后Add to Build path
然后創(chuàng)建NetCacheActivity類(lèi)
package com.Android.xiong.gridlayoutTest; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.android.volley.Request.Method; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; public class NetCacheActivity extends Activity { private static final String URL = "http://sina.com";//請(qǐng)求的url private RequestQueue mRequestQueue; private Button btn_request; private ImageView iv_request; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } private void initView() { // TODO Auto-generated method stub btn_request = (Button) findViewById(R.id.btn_request); iv_request=(ImageView) findViewById(R.id.iv_request); } public void onClick(View v) { //volleyRequest();從網(wǎng)絡(luò)上獲取圖片 imageRequest(); LoadImageView();//ImageLoader加載圖片 } private void imageRequest() { // TODO Auto-generated method stub ImageRequest mImageRequest=new ImageRequest("http://www.bz55.com/uploads/allimg/130716/1-130G6111637.jpg", new Response.Listener<Bitmap>() { //需要的注意的是導(dǎo)入Response.Listener<Bitmap>別導(dǎo)錯(cuò)包! @Override public void onResponse(Bitmap response) { // 將網(wǎng)絡(luò)請(qǐng)求的圖片返回并顯示在ImageView中 try { Thread.sleep(3000);//休眠3秒 iv_request.setImageBitmap(response); Toast.makeText(getApplicationContext(), " onResponse", Toast.LENGTH_SHORT).show(); Log.d(" onResponse", response.toString()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } },0, 0, Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // 默認(rèn)加載圖片資源 iv_request.setImageResource(R.drawable.ic_launcher); Toast.makeText(getApplicationContext(), " onErrorResponse", Toast.LENGTH_SHORT).show(); Log.d(" onErrorResponse", error.toString()); } }); mRequestQueue.add(mImageRequest);//強(qiáng)圖片請(qǐng)求添加到請(qǐng)求隊(duì)列 } public void LoadImageView() { // 利用ImageLoader異步加載圖片 final ImageLoader mImageLoader = new ImageLoader(mquest, new BitmapCache()); ImageListener listener = ImageLoader.getImageListener(iv_request, R.drawable.voice_to_short, R.drawable.ic_launcher); //get請(qǐng)求方式 mImageLoader .get("http://img.my.csdn.NET/uploads/201404/13/1397393290_5765.jpeg", listener); Log.d("ImageLoader", mImageLoader.toString()); } // import com.android.volley.Response.ErrorListener; private void volleyRequest() { StringRequest mRequest = new StringRequest(Method.GET, URL, new Response.Listener<String>() { @Override public void onResponse(String response) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "onResponse ", Toast.LENGTH_LONG).show(); Log.d("on onResponse", response.toString());//請(qǐng)求成功 } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "onErrorResponse", Toast.LENGTH_SHORT).show(); Log.d("on ErrorReponse", error.toString());//請(qǐng)求失敗 } }); mRequestQueue.add(mRequest); } }
BitmapCache
package com.weixin.cache; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import com.android.volley.toolbox.ImageLoader.ImageCache; public class BitmapCache implements ImageCache { private LruCache<String, Bitmap> cache; public BitmapCache() { cache = new LruCache<String, Bitmap>(8 * 1024 * 1024) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; } @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }
布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv_request" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/bitmap" /> <Button android:id="@+id/btn_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:onClick="onClick" android:text="獲取網(wǎng)絡(luò)請(qǐng)求信息" /> </RelativeLayout>
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存
- Android中volley封裝實(shí)踐記錄
- Android Volley框架全面解析
- Android Volley框架使用方法詳解
- Android的HTTP類(lèi)庫(kù)Volley入門(mén)學(xué)習(xí)教程
- Android Volley框架使用源碼分享
- Android中Volley框架下保持會(huì)話(huà)方法
- Android 開(kāi)發(fā)中Volley詳解及實(shí)例
- android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法詳解
- Android中volley封裝實(shí)踐記錄(二)
相關(guān)文章
Android實(shí)現(xiàn)可點(diǎn)擊展開(kāi)的TextView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可點(diǎn)擊展開(kāi)的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Ubuntu中為Android系統(tǒng)上編寫(xiě)Linux內(nèi)核驅(qū)動(dòng)程序?qū)崿F(xiàn)方法
本文主要介紹在Ubuntu 上為Android系統(tǒng)編寫(xiě)Linux內(nèi)核驅(qū)動(dòng)程序, 這里對(duì)編寫(xiě)驅(qū)動(dòng)程序做了詳細(xì)的說(shuō)明,對(duì)研究Android源碼和HAL都有巨大的幫助,有需要的小伙伴可以參考下2016-08-08Android開(kāi)發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁(yè)功能(動(dòng)態(tài)加載指示器)詳解
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁(yè)功能(動(dòng)態(tài)加載指示器),結(jié)合實(shí)例形式詳細(xì)分析了Android使用ViewPager引導(dǎo)頁(yè)的具體步驟,相關(guān)布局、功能使用技巧,需要的朋友可以參考下2017-11-11Android Socket實(shí)現(xiàn)多個(gè)客戶(hù)端聊天布局
這篇文章主要為大家詳細(xì)介紹了Android Socket實(shí)現(xiàn)多個(gè)客戶(hù)端聊天布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android自定義View實(shí)現(xiàn)數(shù)字雨效果的全過(guò)程
小時(shí)候看時(shí)印象最深的就是數(shù)字雨了,導(dǎo)致我現(xiàn)在寫(xiě)代碼也要是黑屏,下面這篇文章主要給大家介紹了關(guān)于Android自定義View實(shí)現(xiàn)數(shù)字雨效果的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02淺談Android應(yīng)用的內(nèi)存優(yōu)化及Handler的內(nèi)存泄漏問(wèn)題
這篇文章主要介紹了Android應(yīng)用的內(nèi)存優(yōu)化及Handler的內(nèi)存泄漏問(wèn)題,文中對(duì)Activity無(wú)法被回收而造成的內(nèi)存泄漏給出了通常的解決方案,需要的朋友可以參考下2016-02-02Android側(cè)滑導(dǎo)航欄的實(shí)例代碼
這篇文章主要介紹了Android側(cè)滑導(dǎo)航欄的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描
這篇文章主要為大家詳細(xì)介紹了Android基于zxing的二維碼網(wǎng)格掃描,仿支付寶網(wǎng)格掃描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03