Volley 的基本使用
来源:互联网 发布:centos 7.0安装教程 编辑:程序博客网 时间:2024/06/06 09:24
简介
volley是2013年Google i/o大会上推出了一个新的网络通信框架它适合于数据量不大,通信频繁的网络操作
在使用volley 之前 首先要 下载jar包 : http://download.csdn.net/detail/q9104422999/9595519 ,
最重要的一点 在 清单文件中要添加访问网络的权限 (很多初学者的朋友都会忘记这一点 , 这样无论怎么请求都是失败的)
<uses-permission android:name="android.permission.INTERNET" />----------
然后我们设置布局文件在这里我们只简单的放了两个按钮做实验
<?xml version="1.0" encoding="utf-8"?><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="match_parent" android:orientation="vertical" tools:context="com.tdcq.myvolleydemo.MainActivity"> <Button android:id="@+id/btn_get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GET请求" /> <Button android:id="@+id/btn_post" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="POST请求" /> <TextView android:id="@+id/response" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="显示响应信息" /> <TextView android:id="@+id/error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:background="@android:color/holo_red_light" android:textColor="@android:color/white" tools:text="显示错误信息" /></LinearLayout>
下面就是进行代码开发了
package com.volley;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;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.StringRequest;import com.android.volley.toolbox.Volley;import java.util.HashMap;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener { public static final String URL_GET = "http://apis.juhe.cn/mobile/get?phone=13888888888&key=daf8fa858c330b22e342c882bcbac622"; public static final String URL_POST = "http://apis.juhe.cn/mobile/get "; private Button btn_get; private Button btn_post; private TextView tv_response; private TextView tv_error; private RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_get = (Button) findViewById(R.id.btn_get); btn_post = (Button) findViewById(R.id.btn_post); tv_response = (TextView) findViewById(R.id.response); tv_error = (TextView) findViewById(R.id.error); btn_get.setOnClickListener(this); btn_post.setOnClickListener(this); requestQueue = Volley.newRequestQueue(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_get: getRequest(); break; case R.id.btn_post: postRequest(); break; default: break; } } private void getRequest() { StringRequest stringRequest = new StringRequest( // 连接的地址 成功的回调 URL_GET, new Response.Listener<String>() { @Override public void onResponse(String response) { tv_response.setText("GET请求结果:" + response); } //失败的回调 }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { tv_error.setText("GET请求错误:" + error.toString()); } }); stringRequest.setTag("Mainactvity"); requestQueue.add(stringRequest); } private void postRequest() { StringRequest request = new StringRequest(Request.Method.POST, URL_POST, new Response.Listener<String>() { @Override public void onResponse(String response) { tv_response.setText("POST请求结果:" + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { tv_error.setText("POST请求错误:" + error.toString()); } }) { /** * 因为在StringRequest中并没有提供设置POST参数的方法 * 所以我们只需要在匿名类中重写getParams()方法,在这里设置POST参数就可以了 */ @Override protected Map<String, String> getParams() throws AuthFailureError { HashMap<String, String> map = new HashMap<>(); map.put("phone", "13812345678"); map.put("key", "daf8fa858c330b22e342c882bcbac622"); return map; } }; request.setTag("Main"); requestQueue.add(request); }}
以上就是一个简单的网络请求 , 后面我们在继续深挖
代码请点击: (http://download.csdn.net/detail/q9104422999/9597024)
1 0
- Volley的基本使用
- Volley的基本使用
- Volley的基本使用
- Volley的基本使用
- Volley 的基本使用
- Volley的基本使用
- Volley的基本用法 使用Volley加载网络图片
- Android Volley使用介绍(一),Volley的基本用法
- Android Volley网络框架的基本使用
- Volley的介绍与基本使用
- Android Volley的认识和基本使用
- Android Volley框架的基本使用解析
- Android Volley基本使用
- Volley的基本用法
- Volley的基本用法
- volley的基本用法
- Volley的基本用法
- Android的HTTP操作库Volley的基本使用教程
- WebRTC学习笔记
- uva10304 Ordering-Tasks 拓扑排序
- 51Nod-1489-蜥蜴和地下室
- SparkMLlib---SGD随机梯度下降算法
- 用递归方式实现二叉树先序、中序、后序遍历
- Volley 的基本使用
- 文章标题
- Android Studio引用第三方jar包及开源library的方法
- 多线程编程入门(16):线程同步工具之CyclicBarrier
- uva10129 欧拉回路 判断出入度数 连通性 遍历压栈
- 【LEETCODE】368- Largest Divisible Subset [Python]
- uva10003 切木棍 线性区间dp
- uva1626 括号序列
- uva1625 颜色的长度