Volley是Android平臺上的網絡通訊庫,能使網絡通訊更快,更簡單,更健壯。java
Volley提供的功能:android
如何使用:將原項目打成jar包直接複製到libs就能夠使用。關鍵是如何將源程序打成jar包。git
1:首先,從git庫先克隆一個下來:apache
git clone https://android.googlesource.com/platform/frameworks/volley
2:將volley編譯成jar包:(個人是Win7+ADT開發環境)json
(1)下載 ant (http://ant.apache.org/),下載以後將其解壓到隨便一盤如 C:\apache-ant-1.9.2windows
(2)配置環境系統變量:api
新建系統變量:緩存
編輯Path:加入 %ANT_HOME%\bin;網絡
(3)測試如下:app
表示成功!
3:執行如下命令進行操做:
若是執行 android update project -p . 提示命令不存在,這時須要將 G:\AndroidADT\adt-bundle-windows-x86-20130219\sdk\tools 加入系統變量。
解決方法以下:
在這裏個人系統中存在倆個虛擬機,在這裏我選擇第一個進行操做:
使用ant進行打jar包:
jar 包存在 \bin\目錄下面。
==================簡單案例
(1)activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="120dip" android:layout_height="120dip" android:layout_marginTop="30dip" /> <ImageView android:id="@+id/imageView" android:layout_width="120dip" android:layout_height="120dip" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical"> <TextView android:id="@+id/tvShowInfo" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </ScrollView> </LinearLayout>
(2)MainActivity.java
package com.example.volleytest; import com.android.volley.Request.Method; import com.android.volley.RequestQueue; import com.android.volley.Response.Listener; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader.ImageCache; import com.android.volley.toolbox.ImageLoader.ImageListener; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.NetworkImageView; import com.android.volley.toolbox.Volley; import android.os.Bundle; import android.support.v4.util.LruCache; import android.widget.ImageView; import android.widget.TextView; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; public class MainActivity extends Activity { private TextView tvShowInfo=null; private ImageView mImageView = null; private NetworkImageView mNetworkImageView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); getJSONByVolley(); loadImageByVolley(); showImageByNetworkImageView(); } private void init(){ tvShowInfo = (TextView)findViewById(R.id.tvShowInfo); mImageView = (ImageView)findViewById(R.id.imageView); mNetworkImageView = (NetworkImageView)findViewById(R.id.networkImageView); } private void getJSONByVolley(){ RequestQueue requestQueue = Volley.newRequestQueue(this); String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json"; final ProgressDialog progressDialog = ProgressDialog.show(this, "This is title", "...Loading..."); requestQueue.add(new JsonObjectRequest(Method.GET,url,null, new Listener(){ @Override public void onResponse(Object arg0) { tvShowInfo.setText(arg0.toString()); progressDialog.dismiss(); } },null)); requestQueue.start(); } private void showImageByNetworkImageView() { String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg"; RequestQueue requestQueue = Volley.newRequestQueue(this); final LruCache<String,Bitmap> lruCache = new LruCache<String,Bitmap>(20); ImageCache imageCache = new ImageCache(){ @Override public void putBitmap(String key, Bitmap value) { lruCache.put(key, value); } @Override public Bitmap getBitmap(String key) { return lruCache.get(key); } }; ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache); mNetworkImageView.setTag("url"); mNetworkImageView.setImageUrl(imageUrl, imageLoader); } private void loadImageByVolley() { String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg"; RequestQueue requestQueue = Volley.newRequestQueue(this); final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20); ImageCache imageCache = new ImageCache() { @Override public void putBitmap(String key, Bitmap value) { lruCache.put(key, value); } @Override public Bitmap getBitmap(String key) { return lruCache.get(key); } }; ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache); ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher); imageLoader.get(imageUrl, listener); } }