Volley tutorial (including instance demo)

Development data

Looking up some materials, I found that many people summed up very well, such as the article about the basic usage of Volley:

Volley combines the advantages of AsyncHttpClient and universal image loader. It can communicate with HTTP as easily as AsyncHttpClient, and load pictures on the network as easily as universal image loader. In addition to being simple and easy to use, the performance of Volley has also been greatly adjusted. Its design goal is to be very suitable for network operations with small data volume, but frequent communication. For network operations with large data volume, such as downloading files, etc., the performance of Volley will be very bad.

The summary of Volley in the following two articles is a manual edition, so it's directly quoted

Thanks to the author of the above article. The following is a small demo that I masturbated by myself through the above article. It is very simple, but it has reference significance for me, so I post it.

Configure dependency / import jar package

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

Using Volley to construct tool classes

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 Tools
 * Created by xuzhuyun on 2018/1/9.
 */

public class HttpUtils {
    private static final String TAG = "HttpUtils";
    /**
     * Declare RequestQueue object
     */
    static RequestQueue mQueue = null;

    /**
     * Declare StringRequest object
     */
    static StringRequest stringRequest = null;

    static JsonObjectRequest mJsonObjectRequest = null;

    /**
     * 1,Get RequestQueue object
     */
    public static RequestQueue getRequestQueue() {
        if (mQueue == null) {
            mQueue = Volley.newRequestQueue(DemoApp.getInstance());
        }
        return mQueue;
    }

    /**
     * 2.Get StringRequest object
     *
     * @param url Requested 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);
            }
        });
    }

    /**
     * Get JsonObjectRequest object
     *
     * @param url Request 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());
            }
        });
    }
}

call

package com.xzy.volleydemo;

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

/**
 * Volley
 * However, it integrates the advantages of AsyncHttpClient and universal image loader. It can communicate with HTTP as easily as AsyncHttpClient,
 * You can also load images on the network as easily as universal image loader. In addition to being simple and easy to use, Volley has also made significant adjustments in performance,
 * Its design goal is to be very suitable for network operations with small amount of data but frequent communication, while for network operations with large amount of data,
 * For example, downloading files and so on, Volley's performance will be very bad.
 */
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);
        /**
         * Request url using http
         * */
         HttpUtils.getRequestQueue().add(HttpUtils.getStringRequest("https://www.baidu.com"));
        /**
         * Request a piece of jsonObject data
         * */
         HttpUtils.getRequestQueue().add(HttpUtils.getJsonObjectRequest("http://www.weather.com.cn/data/sk/101010100.html"));
    }
}

DEMO download

http://download.csdn.net/download/jdfkldjlkjdl/10208079




Tags: Android network JSON

Posted on Tue, 05 May 2020 06:49:28 -0400 by ashwin