1. VollyActivity.java
(1-6 是在手机客户端的程序代码)
import java.util.HashMap;
import java.util.Map;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.wangnan.myhttp.tool.MyApplication;
import com.wangnan.myhttp.tool.PostStringRequest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class VolleyActivity extends Activity implements OnClickListener{
ImageView image;
TextView txt;
private RequestQueue mRequestQueue;
private ImageLoader imageLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.volley);
findViewById(R.id.volleyget).setOnClickListener(this);
findViewById(R.id.volleypost).setOnClickListener(this);
findViewById(R.id.volleydown).setOnClickListener(this);
findViewById(R.id.imageloader).setOnClickListener(this);
findViewById(R.id.myloader).setOnClickListener(this);
txt=(TextView)findViewById(R.id.volleytxt);
image=(ImageView)findViewById(R.id.volleyimg);
MyApplication myApp=(MyApplication)getApplication();
//1.创建请求队列
//使用google下的volley
//mRequestQueue=Volley.newRequestQueue(this);
//自己定义了一个Application
mRequestQueue=myApp.getRequestQueue();
//创建图片加载器
//imageLoader= new ImageLoader(mRequestQueue, new BitMapCache());
imageLoader=myApp.getImageLoader();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//get是从网络上获取
case R.id.volleyget:
//2.定义一个请求Request
StringRequest request = new StringRequest(
"http://www.baidu.com",
//"http://192.16.137.1:8080/topnews/news.do?type=1", //在手机上面运行时(需要保持在同一网络中)
//"http://10.0.0.1/topnews/news.do?type=1", //在模拟器上运行时
new Listener<String>() {
@Override
public void onResponse(String arg0) {
txt.setText(arg0);
}
},
new ErrorListener() {
@Override
public void onErrorResponse(
VolleyError arg0) {
Toast.makeText(VolleyActivity.this, "网络中断", Toast.LENGTH_LONG).show();
}
});
//3.将请求添加到请求队列
mRequestQueue.add(request);
break;
case R.id.volleydown:
ImageRequest imageRequest = new ImageRequest("http://photocdn.sohu.com/20140910/Img404194951.jpg",
new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap arg0) {
image.setImageBitmap(arg0);
}
},
0,
0,
Config.ARGB_8888,
new ErrorListener() {
@Override
public void onErrorResponse(
VolleyError arg0) {
}
});
mRequestQueue.add(imageRequest);
break;
//使用自定义的方法来加载图片, 一个是在加载时显示的图片,一个是加载失败显示的图片(节约了流量和内存)
case R.id.imageloader:
ImageListener listener = ImageLoader.getImageListener(image, R.drawable.ic_launcher, R.drawable.grossini_dance_01);
imageLoader.get("http://photocdn.sohu.com/20140910/Img404194951.jpg", listener);
break;
case R.id.myloader:
PostStringRequest requestPost2 = new PostStringRequest(
Method.POST,
"http://192.168.155.1:8080/TodayTopic/showServlet.do",
new Listener<String>() {
@Override
public void onResponse(String arg0) {
txt.setText(arg0);
}
},
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(VolleyActivity.this, "网络中断", Toast.LENGTH_LONG).show();
}
});
mRequestQueue.add(requestPost2);
break;
default:
break;
}
}
@Override
protected void onStop() {
super.onStop();
mRequestQueue.cancelAll(this);
}
}
2. BitMapCach.java
(由地址转到图片)
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> mCache;
public BitMapCache() {
super();
int maxSize = 1024*1024*10;
mCache = new LruCache<String, Bitmap>(maxSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes()*value.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitMap) {
mCache.put(url, bitMap);
}
}
3. HttpUtitility.java
(设置时间上的限制,超过限定时间就转为提示)
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class HttpUtitility {
public static String doGet(String url){
HttpURLConnection conn=null;
InputStream is=null;
StringBuilder sb=new StringBuilder();
try {
URL httpUrl = new URL(url);
conn=(HttpURLConnection)httpUrl.openConnection();
conn.setConnectTimeout(5*1000);
conn.setReadTimeout(5*1000);
conn.connect();
if(conn.getResponseCode()==200){
is=conn.getInputStream();
byte[] buffer = new byte[1024*10];
int len=0;
while((len=is.read(buffer))!=-1){
sb.append(new String(buffer,0,len));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is!=null)
is.close();
if(conn!=null)
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static Bitmap downImage(String url){
HttpURLConnection conn=null;
InputStream is=null;
Bitmap bitmap=null;
try {
URL httpUrl = new URL(url);
conn=(HttpURLConnection)httpUrl.openConnection();
/*conn.setConnectTimeout(5*1000);
conn.setReadTimeout(5*1000);
conn.connect();*/
if(conn.getResponseCode()==200){
is=conn.getInputStream();
bitmap=BitmapFactory.decodeStream(is);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is!=null)
is.close();
if(conn!=null)
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
}
4. PostStringRequest.java
import java.util.Map;
import com.android.volley.AuthFailureError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
public class PostStringRequest extends StringRequest{
private Map<String, String> map;
public PostStringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
public PostStringRequest(String url, Listener<String> listener,
ErrorListener errorListener) {
super(url, listener, errorListener);
}
public PostStringRequest(String url, Listener<String> listener,
ErrorListener errorListener,Map<String, String> map) {
super(Method.POST, url, listener, errorListener);
this.map=map;
}
public void setParams(Map<String, String> params) {
this.map = params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
5. MyApplication
(一般可以使用Google开发的volly,此处是自己在其基础上修改定义的)
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import android.app.Application;
public class MyApplication extends Application {
private RequestQueue mRequestQueue;
private ImageLoader imageLoader ;
@Override
public void onCreate() {
super.onCreate();
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
imageLoader = new ImageLoader(mRequestQueue, new BitMapCache());
}
public RequestQueue getRequestQueue() {
return mRequestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
6. Volly.xml
(页面形式)
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/volleyget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get_volley"/>
<Button
android:id="@+id/volleypost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/volleyget"
android:text="post_volley"/>
<Button
android:id="@+id/volleydown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/volleypost"
android:text="下载图片"/>
<LinearLayout
android:id="@+id/myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/volleydown">
<Button
android:id="@+id/imageloader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ImageLoader"/>
<Button
android:id="@+id/myloader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="myloader"/>
</LinearLayout>
<TextView
android:id="@+id/volleytxt"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@id/myapp"
android:text="context"/>
<ImageView
android:id="@+id/volleyimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/volleytxt"/>
</RelativeLayout>