Volley框架提供了StringRequest ImageRequest JSONObjectRequest
StringRequest有两种请求方式GET,POST两种方式,用post方式请求可以给服务器传递参数
RequestQueue queue= Volley.newRequestQueue (this);
StringRequest string=new StringRequest(Request.Method.POST,"http://你要访问胡网址", new Response.Listener<String>() {
@Override
public void onResponse(String s) {
TextView textView=findViewById(R.id.money);
textView.setText(s);
Log.d("--v",s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("--v",volleyError.toString());
}
}){
//请求参数
protected Map<String,String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<String,String>();
map.put("openid","9D2470C178E8B88B53E4C1F9AC07A8F7");
return map;
}
};
queue.add(string);
queue.start();
ImageRequest
//访问网络图片图片
String url="网络图片地址"
ImageRequest request=new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
ImageView imageView=findViewById(R.id.img);
imageView.setImageBitmap(bitmap);//设置网络图片
}
}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
queue.add(request);//添加到消息队列中
queue.start();//发起请求
JSONObjectRequest 也有分POST,GET两种请求方式,如果是get方式,jsonRequest参数为null就可以了
//传递的参数
Map map=new HashMap();
map.put("id",13545454);
JSONObject object=new JSONObject(map);
JsonObjectRequest request1=new JsonObjectRequest(Request.Method.POST, "htt://.....", object, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
queue.add(request1);
queue.start();