Android Volley完全解析,初识Volley的基本用法
来源:互联网 发布:阿里的社交软件 编辑:程序博客网 时间:2024/06/14 17:23
1.Volley简介
关于Volley,详情请见权威官网,
Transmitting Network Data Using Volley
郭霖前辈也有相关介绍,请见:
Android Volley完全解析(一),初识Volley的基本用法
2.导入Volley
Volley设置导入不是很方便,希望google能改进这个问题,它不像其它库,添加依赖只需一行代码就可以。它比较复杂,
首先,git clone Volley的github仓库。
git clone https://android.googlesource.com/platform/frameworks/volley
可能你会遇到git的代理问题,没关系,遇到问题很正常,希望的一篇文章可以帮到你。
git clone Volley Failed?git 代理!
第二导入git clone 的Volley作为Android Library.
关于如何操作,下面链接是权威指导,如果打不开,那可能你需要一个科学上网代理工具。
Create an Android Library
在Android Studio中。File->New->Import Moudule
把你的Volley目录放进去,点击Finish.
第三,把作为依赖项。按着快捷键 Shift+Ctrl+Alt+S,选中Modules下面的app,点击Dependenices.如图
选中Volley,点击ok即可。
Add the INTERNET Permission
要使用Volley,首先
<uses-permission android:name="android.permission.INTERNET"/>
否则是不能联网的哦。
Use newRequestQueue
创建一个RequestQueue对象
final TextView mTextView = (TextView) findViewById(R.id.test_text); //Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url = "http://www.baidu.com";
创建一个StringRequest对象
//Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Display the first 500 characters of the response string. mTextView.setText("Response is: "+ response.substring(0,500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mTextView.setText("That didn't work!"); } });
将这个StringRequest对象添加到RequestQueue里面
// Add the request to the RequestQueue. queue.add(stringRequest);
完整代码
MainAcitvity
package com.lizheng.www.volleytest2;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;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;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView mTextView = (TextView) findViewById(R.id.test_text); //Instantiate the RequestQueue.你可能会发现报错cannot resolve symbol requestqueue //add following to your build.gradle file compile 'com.mcxiaoke.volley:library:1.0.19' RequestQueue queue = Volley.newRequestQueue(this); String url = "http://www.baidu.com"; //Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Display the first 500 characters of the response string. mTextView.setText("Response is: "+ response.substring(0,500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mTextView.setText("That didn't work!"); } }); // Add the request to the RequestQueue. queue.add(stringRequest); }}
activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.lizheng.www.volleytest2.MainActivity"> <TextView android:id="@+id/test_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /></RelativeLayout>
0 0
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- Android Volley完全解析(一),初识Volley的基本用法
- 2016湖南省程序设计大赛acm : A题
- 关于LeetCode中Ransom Note一题的理解
- TP框架excel导入
- logstash 学习二 input 配置
- 弧度和度 180/PI PI/180换算关系
- Android Volley完全解析,初识Volley的基本用法
- mysql删除指定数据库所有的表
- Java反射机制深入详解
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
- [转]【啊哈!算法】算法11:堆——神奇的优先队列(上)--作者:ahalei
- 第一次跑高光谱数据结果
- MongoDB分片实战(三):性能和优化
- java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject
- 【题】【状压&搜索】NKOJ 3751 扫雷游戏