您的位置:首页 > 理论基础 > 计算机网络

Android学习笔记:Andorid网络请求框架Volley的使用(上)

2015-08-25 15:53 701 查看
Volley框架是Google I/O 2013大会上发布的。Volley是Google针对Android平台上的网络通信库而诞生,该框架能使网络通信更快,更简单,更健壮。Volley的特点有:

Ⅰ:通信更简单更快捷
ll:Get,Post网络请求及网络图像进行高效异步处理
III:可以对多个网络请求进行优先级排序以级多级别取消操作
IV:网络请求缓存及与Activity生命周期进行联动,在Activity销毁的时候可以对网络请求进行取消操作,防止内存泄露。
Volley的使用很简单:
1,下载Volley的jar包或是源代码,github地址:https://github.com/stormzhang/AndroidVolley
2,获取RequestQueue,一般我们是放到Application中进行初始化操作

3,实例化一个Request对象(StringRequest,JsonObjectRequest,JsonArrayRequest)

4,将Request加入RequestQueue即可。

首先学习的是最基础的StringRequest:

主页面布局文件:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:layout_margin="10dp" >

<Button

android:id="@+id/get_btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="GET请求"

android:textSize="16sp"

android:layout_marginTop="15dp"/>

<Button

android:id="@+id/post_btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="POST请求"

android:textSize="16sp" />

</LinearLayout>

-------------请求操作------------------------

public class MainActivity extends Activity implements OnClickListener {

private Button getBtn, postBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

getBtn = (Button) findViewById(R.id.get_btn);

postBtn = (Button) findViewById(R.id.post_btn);

getBtn.setOnClickListener(this);

postBtn.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.get_btn:// GET请求获取数据

getStringRequest();

break;

case R.id.post_btn:// POST请求获取数据

postStringequest();

break;

}

}

private void getStringRequest() {// 用GET方法使用字符串数据请求StringRequest

String url = "http://apis.juhe.cn/mobile/get?phone=134****4320&key=d6099881640aed5e0bacc058cfd4357b";//key值是在聚合数据上申请的api

StringRequest strRequest = new StringRequest(Method.GET, url, new Listener<String>() {

@Override

public void onResponse(String arg0) {// 请求成功

Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();

}

}, new ErrorListener() {

@Override

public void onErrorResponse(VolleyError arg0) {// 请求失败

Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();

}

});

strRequest.setTag("getLDM");

MyApplication.getRequestQueues().add(strRequest);

}

private void postStringequest() {// 用POST方法使用字符串数据请求StringRequest

String url = "http://apis.juhe.cn/mobile/get?";

StringRequest postRequest = new StringRequest(Method.POST, url, new Listener<String>() {

@Override

public void onResponse(String arg0) {

Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();

}

}, new ErrorListener() {

@Override

public void onErrorResponse(VolleyError arg0) {

Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();

}

}) {

@Override

protected Map<String, String> getParams() throws AuthFailureError {// getparams方法是Volley中使用POST请求数据时传递参数信息

Map<String, String> map = new HashMap<String, String>();

map.put("phone", "134****4320");

map.put("key", "d6099881640aed5e0bacc058cfd4357b");

return map;

}

};

postRequest.setTag("postLDM");

MyApplication.getRequestQueues().add(postRequest);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: