Volley教程(含实例demo)

开发资料

查阅了一些资料,发现很多人总结得很好,比如介绍Volley基本用法的文章:

Volley 可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整, 它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。

下面这两篇文章对Volley的总结堪称手册版,所以直接引用过来了

感谢以上文章的作者。下面是我通过上面的文章自己撸的一个小demo,非常简单,但是对我有参考意义,所以贴出来。

配置依赖/引入jar包

 implementation files('libs/volley.jar')
或者
compile 'com.mcxiaoke.volley:library:1.0.19'

使用Volley构造工具类

package com.xzy.volleydemo;

import android.util.Log;

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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

/**
 * Function:volley工具类
 * Created by xuzhuyun on 2018/1/9.
 */

public class HttpUtils {
    private static final String TAG = "HttpUtils";
    /**
     * 声明RequestQueue对象
     */
    static RequestQueue mQueue = null;

    /**
     * 声明StringRequest对象
     */
    static StringRequest stringRequest = null;

    static JsonObjectRequest mJsonObjectRequest = null;

    /**
     * 1、获取RequestQueue对象
     */
    public static RequestQueue getRequestQueue() {
        if (mQueue == null) {
            mQueue = Volley.newRequestQueue(DemoApp.getInstance());
        }
        return mQueue;
    }

    /**
     * 2.获取StringRequest对象
     *
     * @param url 请求的url
     * @return StringRequest
     */
    public static StringRequest getStringRequest(String url) {
        return stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.getMessage(), error);
            }
        });
    }

    /**
     * 获取JsonObjectRequest对象
     *
     * @param url 请求url
     * @return JsonObjectRequest
     */
    public static JsonObjectRequest getJsonObjectRequest(String url) {
        return mJsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("JsonObjectRequest", response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("JsonObjectRequest", "onErrorResponse: " + volleyError.getMessage());
            }
        });
    }
}

调用

package com.xzy.volleydemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Volley
 * 可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,
 * 也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整,
 * 它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,
 * 比如说下载文件等,Volley的表现就会非常糟糕。
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.tv_test);
        /**
         * 使用http请求url
         * */
         HttpUtils.getRequestQueue().add(HttpUtils.getStringRequest("https://www.baidu.com"));
        /**
         * 请求一段jsonObject数据
         * */
         HttpUtils.getRequestQueue().add(HttpUtils.getJsonObjectRequest("http://www.weather.com.cn/data/sk/101010100.html"));
    }
}

DEMO下载





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值