第一步:导依赖
dependencies { compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28' }
第二步代码实现
1. 创建一个RequestQueue对象。
2. 创建一个StringRequest对象。
3. 将StringRequest对象添加到RequestQueue里面。
package com.example.myvolley;
import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast;
import com.android.volley.AuthFailureError; 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.ImageRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley;
import java.util.HashMap; import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button but1;
private Button but2;
private ImageView img1;
private Button but3;
private ImageView img2;
private Button but4;
public static final String URLPOSTSTRING = "http://172.16.45.49:8080/day13/servlet/demo";
public static final String URLIMG = "http://img.my.csdn.net/uploads/201403/03/1393854083\_6511.jpg";
public static final String URLSTRING = "http://112.124.22.238:8081/course\_api/cniaoplay/featured2?p={%22page%22:0}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\_main);
initView();
}
private void initView() {
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
img1 = (ImageView) findViewById(R.id.img1);
but3 = (Button) findViewById(R.id.but3);
img2 = (ImageView) findViewById(R.id.img2);
but4 = (Button) findViewById(R.id.but4);
but1.setOnClickListener(this);
but2.setOnClickListener(this);
but3.setOnClickListener(this);
but4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.but1:
requestStringGet();
break;
case R.id.but2:
requsetImgGet();
break;
case R.id.but3:
requsetImgLoaderGet();
break;
case R.id.but4:
requsetPost();
break;
}
}
//POST请求字符串
private void requsetPost() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLPOSTSTRING, new Response.Listener
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("username", "abc");
map.put("password", "123");
return map;
}
};
requestQueue.add(stringRequest);
}
//Get请求图片--缩放模式
private void requsetImgLoaderGet() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String url) {
return null;
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
}
});
ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(img2,R.mipmap.ic_launcher, R.mipmap.ic_launcher);
imageLoader.get(URLIMG,imageListener);
}
//Get请求图片
private void requsetImgGet() {
RequestQueue requestQueue =Volley.newRequestQueue(this);
ImageRequest imageRequest = new ImageRequest(URLIMG, new Response.Listener
}
});
requestQueue.add(imageRequest);
}
//Get请求字符串
private void requestStringGet() {
RequestQueue requestqueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET,URLSTRING,
new Response.Listener
}
});
requestqueue.add(stringRequest);
}
}