标签:
一.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<String, String> 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 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content"> 16 <Button 17 android:layout_width="0dp" 18 android:layout_height="wrap_content" 19 android:layout_weight="1" 20 android:text="JDK-Post方式" 21 android:onClick="bt2_OnClick"/> 22 <Button 23 android:layout_width="0dp" 24 android:layout_height="wrap_content" 25 android:layout_weight="1" 26 android:text="JDK-Get方式" 27 android:onClick="bt1_OnClick"/> 28 </LinearLayout> 29 30 <LinearLayout 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content"> 33 <Button 34 android:layout_width="0dp" 35 android:layout_height="wrap_content" 36 android:layout_weight="1" 37 android:text="Volley-Get方式" 38 android:onClick="bt3_OnClick"/> 39 <Button 40 android:layout_width="0dp" 41 android:layout_height="wrap_content" 42 android:layout_weight="1" 43 android:text="Volley-Post方式" 44 android:onClick="bt4_OnClick"/> 45 </LinearLayout> 46 <EditText 47 android:layout_width="match_parent" 48 android:layout_height="200dp" 49 android:id="@+id/et_2"/> 50 51 </LinearLayout>
1 package com.hanqi.testapp3; 2 3 import android.app.ProgressDialog; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 import com.android.volley.AuthFailureError; 12 import com.android.volley.Request; 13 import com.android.volley.RequestQueue; 14 import com.android.volley.Response; 15 import com.android.volley.VolleyError; 16 import com.android.volley.toolbox.StringRequest; 17 import com.android.volley.toolbox.Volley; 18 19 import java.io.InputStream; 20 import java.io.OutputStream; 21 import java.net.HttpURLConnection; 22 import java.net.URL; 23 import java.util.HashMap; 24 import java.util.Map; 25 26 public class TestActivity3 extends AppCompatActivity { 27 28 29 EditText et_2; 30 31 //请求队列 32 RequestQueue requestQueue; 33 34 @Override 35 protected void onCreate(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 void bt3_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<String>() { 55 @Override 56 public void onResponse(String s) { 57 //处理正常响应 58 //在主线程响应 59 et_2.setText(s); 60 61 pd.dismiss(); 62 } 63 }, 64 new Response.ErrorListener() { 65 @Override 66 public void onErrorResponse(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 void bt4_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<String>() { 92 @Override 93 public void onResponse(String s) { 94 //处理正常响应 95 //在主线程响应 96 et_2.setText(s); 97 98 pd.dismiss(); 99 } 100 }, 101 new Response.ErrorListener() { 102 @Override 103 public void onErrorResponse(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 @Override 116 protected Map<String, String> getParams() throws AuthFailureError { 117 118 Map<String,String> rtn =new HashMap<>(); 119 120 rtn.put("name","postVolley"); 121 rtn.put("password","567"); 122 123 return rtn; 124 } 125 }; 126 //加入队列 127 requestQueue.add(stringRequest); 128 129 } 130 }
标签:
原文地址:http://www.cnblogs.com/cycanfly/p/5585902.html