androidにおけるvollyの応用
17427 ワード
VollyはAynchttp ClidentとUniversal-Ingel-Leoaderの長所を一身に集めたものと言えます.Asynchttp Clidentのように簡単にHTTP通信ができますし、Universal-Ingel-Link aderのようにインターネット上の画像を簡単にロードすることもできます.簡単で使いやすいだけでなく、性能面でも大幅に調整されたVollyの設計目標は、データ量は少ないが、通信が頻繁なネットワーク操作で、大きなデータ量のネットワーク操作、例えばファイルのダウンロードなど、Vollyの表は非常にまずいことになる.
ここでは一例を使ってListViewで使用されているvollyの画像ロードを実証します.
ホームページレイアウト:activityvolly_Jsonイメージ.イメージtest.xml
http://blog.csdn.net/guolin_ブログ/アート/detail/17482095
ここでは一例を使ってListViewで使用されているvollyの画像ロードを実証します.
ホームページレイアウト:activityvolly_Jsonイメージ.イメージtest.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">
<ListView
android:id="@+id/lv_volley"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
<TextView
android:id="@+id/txt_info"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=" ......"
android:textSize="18sp"
android:gravity="center"
android:visibility="gone"/>
</LinearLayout>
ListView item
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/img_pic"
android:layout_width="100dp"
android:layout_height="100dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:text=" "
android:gravity="center_vertical"/>
<TextView
android:id="@+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:text="2015-03-12"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
プログラムコード:Volly_Jsonイメージ.イメージtestpackage com.example.cg.fangduo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Volley_Json_image_test extends Activity {
// listview
private ListView lv_volley;
//
private TextView txt_info;
// volley
public RequestQueue mQueue;
//
public List<Map<String, Object>> info;
// Adapter;
public Volley_Json_image_Adapter volleyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volley__json_image_test);
initView();
}
/**
*
*/
private void initView()
{
// listview, , , , listview
lv_volley = (ListView)this.findViewById(R.id.lv_volley);
lv_volley.setOnScrollListener(new ScrollListener());
lv_volley.setVisibility(View.GONE);
// , ,
txt_info = (TextView)this.findViewById(R.id.txt_info);
txt_info.setVisibility(View.VISIBLE);
// volley
mQueue = Volley.newRequestQueue(this);
//
// [{"notepic":"http://image.baidu.com/i?ct=503316480&tn=baiduimagedetail&statnum=girl&ipn=d&z=0&fr=ala&s=0&ic=0&lm=-1&itg=0&cg=girl&word=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&ie=utf-8&in=3354&cl=2&st=&pn=6&rn=1&di=&ln=31000&&fmq=1378374347070_R&se=&sme=0&tab=&face=&&is=0,132526&cs=0,0&adpicid=0&pi=14147590090&os=0&istype=&ist=&jit=&objurl=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F377adab44aed2e7341c3f0578401a18b87d6faa7.jpg","noteTypeid":1,"noteid":1,"noteFast":0,"noteTitle":" ","noteDateTime":"2014-7-6"},
// {"notepic":"http://img3.imgtn.bdimg.com/it/u=2638575563,3145336706&fm=23&gp=0.jpg","noteTypeid":1,"noteid":2,"noteFast":0,"noteTitle":" ","noteDateTime":"2014-8-17"},
// {"notepic":"http://img1.imgtn.bdimg.com/it/u=2816850553,3679993090&fm=23&gp=0.jpg","noteTypeid":1,"noteid":3,"noteFast":0,"noteTitle":" ","noteDateTime":"2014-8-17"},
// {"notepic":"http://img4.imgtn.bdimg.com/it/u=793795634,3401984036&fm=23&gp=0.jpg","noteTypeid":1,"noteid":4,"noteFast":0,"noteTitle":" 1","noteDateTime":"2014-8-29"},
// {"notepic":"http://img4.imgtn.bdimg.com/it/u=2978660446,2856475006&fm=23&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 2","noteDateTime":"2014-8-29"},
// {"notepic":"http://img5.imgtn.bdimg.com/it/u=1111035714,1878413225&fm=23&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 3","noteDateTime":"2014-8-29"},
// {"notepic":"http://img1.imgtn.bdimg.com/it/u=3981637234,1431600967&fm=23&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 4","noteDateTime":"2014-8-29"},
// {"notepic":"http://img3.imgtn.bdimg.com/it/u=1471976729,3245530376&fm=23&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 5","noteDateTime":"2014-8-29"},
// {"notepic":"http://img1.imgtn.bdimg.com/it/u=992411909,4001832108&fm=23&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 6","noteDateTime":"2014-8-29"},
// {"notepic":"http://img3.imgtn.bdimg.com/it/u=1438096542,2010064131&fm=23&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 7","noteDateTime":"2014-8-29"},
// {"notepic":"http://img1.imgtn.bdimg.com/it/u=2348235091,3744783718&fm=21&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 8","noteDateTime":"2014-8-29"},
// {"notepic":"http://img3.imgtn.bdimg.com/it/u=486736756,3902182809&fm=21&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 9","noteDateTime":"2014-8-29"},
// {"notepic":"http://img2.imgtn.bdimg.com/it/u=2671166332,507275986&fm=21&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 10","noteDateTime":"2014-8-29"},
// {"notepic":"http://img5.imgtn.bdimg.com/it/u=1857438318,1706082473&fm=21&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 11","noteDateTime":"2014-8-29"},
// {"notepic":"http://img0.imgtn.bdimg.com/it/u=547181070,2589863976&fm=21&gp=0.jpg","noteTypeid":1,"noteid":5,"noteFast":0,"noteTitle":" 12","noteDateTime":"2014-8-29"},
// {"notepic":"http://img4.imgtn.bdimg.com/it/u=1319733707,1742255567&fm=11&gp=0.jpg","noteTypeid":1,"noteid":6,"noteFast":0,"noteTitle":" 13","noteDateTime":"2014-8-29"}]
StringRequest jsonRequest = new StringRequest(Request.Method.GET,"http://172.31.0.163:9090/VolleyData.ashx?flag=1",
new Response.Listener<String>(){
@Override
public void onResponse(String jsonObject) {
txt_info.setVisibility(View.GONE);
lv_volley.setVisibility(View.VISIBLE);
Log.v("Volley_Json_image_test:",jsonObject.toString());
info = getlistForJson(jsonObject);
volleyAdapter = new Volley_Json_image_Adapter(Volley_Json_image_test.this,info);
lv_volley.setAdapter(volleyAdapter);
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("onErrorResponse:",volleyError.toString());
}
});
mQueue.add(jsonRequest);
//mQueue.start();
}
/**
* ListViwe
*/
class ScrollListener implements AbsListView.OnScrollListener
{
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastItemid = lv_volley.getLastVisiblePosition();
if((lastItemid + 1) == totalItemCount)
{
// 0( 30 , 。 30 )
if(totalItemCount < 30 && totalItemCount > 0) {
//
StringRequest jsonRequest2 = new StringRequest(Request.Method.GET, "http://172.31.0.163:9090/VolleyData.ashx?flag=2",
new Response.Listener<String>() {
@Override
public void onResponse(String jsonObject) {
info.addAll(getlistForJson(jsonObject));
volleyAdapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("onErrorResponse:", volleyError.toString());
}
});
mQueue.add(jsonRequest2);
}
}
}
}
/**
* Json Map<>
* @param jsonStr
* @return
*/
public static Map<String, Object> getMapForJson(String jsonStr){
JSONObject jsonObject ;
try {
jsonObject = new JSONObject(jsonStr);
Iterator<String> keyIter= jsonObject.keys();
String key;
Object value ;
Map<String, Object> valueMap = new HashMap<String, Object>();
while (keyIter.hasNext()) {
key = keyIter.next();
value = jsonObject.get(key);
valueMap.put(key, value);
}
return valueMap;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
//Log.e(HttpClientUtils.TAG, e.toString());
}
return null;
}
/**
* Json List<Map<>>
* @param jsonStr
* @return
*/
public static List<Map<String, Object>> getlistForJson(String jsonStr){
List<Map<String, Object>> list = null;
try {
JSONArray jsonArray = new JSONArray(jsonStr);
JSONObject jsonObj ;
list = new ArrayList<Map<String,Object>>();
for(int i = 0 ; i < jsonArray.length() ; i ++){
jsonObj = (JSONObject)jsonArray.get(i);
list.add(getMapForJson(jsonObj.toString()));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
}
listViewのAdapterのコード:Volly_Jsonイメージ.イメージAdapterpackage com.example.cg.fangduo;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import java.util.List;
import java.util.Map;
/**
* Created by cg on 2015/3/12.
*/
public class Volley_Json_image_Adapter extends BaseAdapter {
private LayoutInflater inflater;
//
private List<Map<String, Object>> jsonInfo;
// volley
private RequestQueue mQueue;
private ImageLoader imageLoader;
public Volley_Json_image_Adapter(Context context, List<Map<String, Object>> jsonInfo)
{
this.jsonInfo = jsonInfo;
this.inflater = LayoutInflater.from(context);
mQueue = Volley.newRequestQueue(context);
imageLoader = new ImageLoader(mQueue,new BitmapCache(){
@Override
public Bitmap getBitmap(String s) {
return null;
}
@Override
public void putBitmap(String s, Bitmap bitmap) {
}
});
}
@Override
public int getCount() {
return jsonInfo.size();
}
@Override
public Object getItem(int position) {
return jsonInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
VolleyInfo volleyInfo;
if(convertView==null)
{
volleyInfo = new VolleyInfo();
convertView = inflater.inflate(R.layout.lv_volley_item, null);
volleyInfo.img_pic = (NetworkImageView)convertView.findViewById(R.id.img_pic);
volleyInfo.txt_title = (TextView)convertView.findViewById(R.id.txt_title);
volleyInfo.txt_time = (TextView)convertView.findViewById(R.id.txt_time);
convertView.setTag(volleyInfo);
}else
{
volleyInfo = (VolleyInfo)convertView.getTag();
}
volleyInfo.txt_title.setText(jsonInfo.get(position).get("noteTitle").toString());
volleyInfo.txt_time.setText(jsonInfo.get(position).get("noteDateTime").toString());
// , NetworkImageView 。
// , ,
// ,
//
volleyInfo.img_pic.setDefaultImageResId(R.drawable.noimg);
volleyInfo.img_pic.setErrorImageResId(R.drawable.a01);
volleyInfo.img_pic.setImageUrl(jsonInfo.get(position).get("notepic").toString(),imageLoader);
return convertView;
}
public class VolleyInfo
{
NetworkImageView img_pic;
TextView txt_title;
TextView txt_time;
}
/**
* OOM
*/
public class BitmapCache implements ImageLoader.ImageCache{
private LruCache<String,Bitmap> mCache;
public BitmapCache(){
int maxSize = 10*1024*1024;
mCache = new LruCache<String, Bitmap>(maxSize){
protected int sizeOf(String key,Bitmap bitmap)
{
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url,bitmap);
}
}
}
Vollyについてもっと詳しく知りたいなら、参照してください.http://blog.csdn.net/guolin_ブログ/アート/detail/17482095