Android 网络通信框架Volley的简单使用

  Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。

Volley提供的功能:

  • JSON,图像等的异步下载;
  • 网络请求的排序(scheduling)
  • 网络请求的优先级处理
  • 缓存
  • 多级别取消请求
  • 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)

如何使用:将原项目打成jar包直接复制到libs就可以使用。关键是如何将源程序打成jar包。

1:首先,从git库先克隆一个下来:

git clone https://android.googlesource.com/platform/frameworks/volley  

2:将volley编译成jar包:(我的是Win7+ADT开发环境)

(1)下载 ant (http://ant.apache.org/),下载之后将其解压到随便一盘如  C:apache-ant-1.9.2

(2)配置环境系统变量:

新建系统变量:

编辑Path:加入 %ANT_HOME%in;

(3)测试以下:

表示成功!

3:执行以下命令进行操作:

如果执行 android update project -p . 提示命令不存在,这时需要将 G:AndroidADTadt-bundle-windows-x86-20130219sdk ools 加入系统变量。

解决方法如下:

在这里我的系统中存在俩个虚拟机,在这里我选择第一个进行操作:

使用ant进行打jar包:

jar 包存在 in目录下面。

==================简单案例

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

    }
}
原文地址:https://www.cnblogs.com/yshyee/p/3414883.html