一.Volley
1.特点
①轻量级的Android网络通信库
②适合数量不大但通信频繁的场景
2.API
1.RequestQueue
①请求队列
②Volley.newRequestQueue(context) 创建或得到请求队列 单例模式
③add(Request)添加请求到队列中
2.Request
①封装请求的基类
②子类
1)StringRequest 返回字符串的请求
构造方法:
new StringRequest(get_path,正常响应监听,异常响应监听) 实现get方式
new StringRequest(Request.Method.post,get_path,正常响应监听,异常响应监听){重写 Map getParams()} 实现post方式
2)JsonRequest 返回Json的请求
3)ImageRequest 返回图片的请求
③Request.Method
1)GET 2)POST
二.加载方法
1.把jar包放到模块的libs目录下(切换到project视图模式)
2.在jar包上鼠标右键单机,选择“add as lib”,加入到开发环境
Volley-Get方式
Volley-Post方式
1 <?xml version="1.0" encoding="utf-8"?>
2
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context="com.hanqi.testapp3.TestActivity3"
11 android:orientation="vertical">
12
13
15 android:layout_height="wrap_content">
16
18 android:layout_height="wrap_content"
19 android:layout_weight="1"
20 android:text="JDK-Post方式"
21 android:onClick="bt2_OnClick"/>
22
24 android:layout_height="wrap_content"
25 android:layout_weight="1"
26 android:text="JDK-Get方式"
27 android:onClick="bt1_OnClick"/>
28
29
30
32 android:layout_height="wrap_content">
33
35 android:layout_height="wrap_content"
36 android:layout_weight="1"
37 android:text="Volley-Get方式"
38 android:onClick="bt3_OnClick"/>
39
41 android:layout_height="wrap_content"
42 android:layout_weight="1"
43 android:text="Volley-Post方式"
44 android:onClick="bt4_OnClick"/>
45
46
48 android:layout_height="200dp"
49 android:id="@+id/et_2"/>
50
51
.xml
1 packagecom.hanqi.testapp3;2
3 importandroid.app.ProgressDialog;4 importandroid.os.Bundle;5 importandroid.support.v7.app.AppCompatActivity;6 importandroid.util.Log;7 importandroid.view.View;8 importandroid.widget.EditText;9 importandroid.widget.Toast;10
11 importcom.android.volley.AuthFailureError;12 importcom.android.volley.Request;13 importcom.android.volley.RequestQueue;14 importcom.android.volley.Response;15 importcom.android.volley.VolleyError;16 importcom.android.volley.toolbox.StringRequest;17 importcom.android.volley.toolbox.Volley;18
19 importjava.io.InputStream;20 importjava.io.OutputStream;21 importjava.net.HttpURLConnection;22 importjava.net.URL;23 importjava.util.HashMap;24 importjava.util.Map;25
26 public class TestActivity3 extendsAppCompatActivity {27
28
29 EditText et_2;30
31 //请求队列
32 RequestQueue requestQueue;33
34 @Override35 protected voidonCreate(Bundle savedInstanceState) {36 super.onCreate(savedInstanceState);37 setContentView(R.layout.activity_test3);38
39 et_2=(EditText)findViewById(R.id.et_2);40
41 //创建Volley的请求队列42 //在子线程
43 requestQueue= Volley.newRequestQueue(this);44
45 }46 //Volley的Get
47 public voidbt3_OnClick(View v)48 {49 //1.启动进度对话框
50 final ProgressDialog pd = ProgressDialog.show(this, null, "正在加载,请稍后...");51
52 //构建StringRequest
53 StringRequest stringRequest = new StringRequest("http://192.168.0.128:81/index.asp?name=volley&password=456",54 new Response.Listener() {55 @Override56 public voidonResponse(String s) {57 //处理正常响应58 //在主线程响应
59 et_2.setText(s);60
61 pd.dismiss();62 }63 },64 newResponse.ErrorListener() {65 @Override66 public voidonErrorResponse(VolleyError volleyError) {67
68 //处理异常响应
69 volleyError.printStackTrace();70
71 pd.dismiss();72 Toast.makeText(TestActivity3.this, "响应异常 响应状态码" +volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();73
74 }75 });76 //加入队列
77 requestQueue.add(stringRequest);78
79 }80
81
82
83 //Volley的Post
84 public voidbt4_OnClick(View v)85 {86 //1.启动进度对话框
87 final ProgressDialog pd = ProgressDialog.show(this, null, "正在加载,请稍后...");88
89 //构建StringRequest
90 StringRequest stringRequest = new StringRequest(Request.Method.POST,"http://192.168.0.128:81/index.asp",91 new Response.Listener() {92 @Override93 public voidonResponse(String s) {94 //处理正常响应95 //在主线程响应
96 et_2.setText(s);97
98 pd.dismiss();99 }100 },101 newResponse.ErrorListener() {102 @Override103 public voidonErrorResponse(VolleyError volleyError) {104
105 //处理异常响应
106 volleyError.printStackTrace();107
108 pd.dismiss();109 Toast.makeText(TestActivity3.this, "响应异常 响应状态码" +volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();110
111 }112 }){113
114 //重写 得到参数的方法
115 @Override116 protected Map getParams() throwsAuthFailureError {117
118 Map rtn =new HashMap<>();119
120 rtn.put("name","postVolley");121 rtn.put("password","567");122
123 returnrtn;124 }125 };126 //加入队列
127 requestQueue.add(stringRequest);128
129 }130 }
.java