volley 應用 GET POST請求 圖片異步加載

package com.example.volleydemo;html

import java.util.HashMap;
import java.util.Map;java

import org.json.JSONObject;android

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;git

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
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.StringRequest;
import com.android.volley.toolbox.Volley;json

/**
* Demo描述: 1 利用Volley獲取JSON數據 2 利用Volley異步加載圖片 1 利用NetworkImageView加載網路圖片
*
*/
public class MainActivity extends Activity {
private ImageView mImageView;
private NetworkImageView mNetworkImageView;api

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}網絡

private void init() {
mImageView = (ImageView) findViewById(R.id.imageView);
mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);
getJSONByVolley();
getJsonPost();
loadImageByVolley();
showImageByNetworkImageView();
}app

/**
* 利用Volley獲取JSON數據
*/
private void getJSONByVolley() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String JSONDataUrl = "http://127.0.0.1:8099/api/api.ashx?action=product_list&ver=1&pageindex=1&pageCount=5";
final ProgressDialog progressDialog = ProgressDialog.show(this,
"請稍後...", "正在加載...");異步

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, JSONDataUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//處理返回的JSON數據
Log.e("bbb", response.toString());
if(progressDialog.isShowing() && progressDialog!=null){
progressDialog.dismiss(); }
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// System.out.println("sorry,Error");
Log.e("aaa", arg0.toString());
}
});
requestQueue.add(jsonObjectRequest);
}ide

/***
* 以POST形式加載數據
*/
private void getJsonPost() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://127.0.0.1:8099/api/api.ashx";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//處理返回的JSON數據
Log.d("Response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
}
}) {
@Override
protected Map<String, String> getParams() {
//POST 參數
Map<String, String> params = new HashMap<String, String>();
params.put("action", "product_list");
params.put("ver", "1");
params.put("pageindex", "1");
params.put("pageCount", "5");
return params;
}
};
requestQueue.add(postRequest);
}

/**
* 利用Volley異步加載圖片
*
* 注意方法參數: getImageListener(ImageView view, int defaultImageResId, int
* errorImageResId) 第一個參數:顯示圖片的ImageView 第二個參數:默認顯示的圖片資源 第三個參數:加載錯誤時顯示的圖片資源
*/
private void loadImageByVolley() {
String imageUrl = "http://d.hiphotos.baidu.com/pic/w%3D310/sign=a00aca825aafa40f3cc6c8dc9b65038c/060828381f30e9246b63e1814c086e061c95f7bf.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);
}

/**
* 利用NetworkImageView顯示網絡圖片
*/
private void showImageByNetworkImageView() {
String imageUrl = "http://d.hiphotos.baidu.com/pic/w%3D310/sign=a00aca825aafa40f3cc6c8dc9b65038c/060828381f30e9246b63e1814c086e061c95f7bf.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);
}

}

 

 

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

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
/>

<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>

</RelativeLayout>

 

引入Volley很是簡單,首先,從git庫先克隆一個下來:

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

 

而後生成JAR包 引用

下面是我生成好的JAR

引用volley JAR包 下載地址

 

結合GSON和fastJson用法

http://www.cnblogs.com/freexiaoyu/p/3955137.html

相關文章
相關標籤/搜索