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
原创粉丝点击