欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解

 更新時(shí)間:2017年06月26日 10:28:30   作者:KdanMin  
這篇文章主要介紹了Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

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ì)本站的支持!

相關(guān)文章

最新評(píng)論