Android development: Volley sends Http request

Volley is a module that encapsulates the jar package, which can easily send and process http requests.

You need to download Volley.java first. Download address: Click to download,
then copy Volley.jar to the project, and then add to library.

RequestQueue: literally means a request queue. Since RequestQueue has handled high concurrency internally, we can share a RequestQueue [request queue] for all requests.

StringRequest: specific request information.
In MainActivity.java:

RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest("http://www.baidu.com",
        new Response.Listener<String>() {
            @Override
            public void onResponse(String string) {
                Log.d("MainActivity", string);
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("MainActivity", volleyError.getMessage(), volleyError);
                }
            }
    );
requestQueue.add(stringRequest);

The effect is as follows:
Write picture description here
simply send an http request to get the source code of Baidu's homepage. That's it.

Send a POST request:

RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://115.159.30.17/IHelpYou/Login",
                new Response.Listener<String>() {
                    public void onResponse(String response) {
                        Log.d("TAG", response);
                    }
                },
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                    }
                }
            ) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> map = new HashMap<>();
                    map.put("phone", "13530536529");
                    map.put("password", "123456");
                    return map;
                }
        };
        requestQueue.add(stringRequest);

In this case, the effect can be much higher.

More importantly, Volley supports json data:

RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=%E7%99%BE%E5%BA%A6", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        requestQueue.add(jsonObjectRequest);

Effect:
Write picture description here
Of course, you can also override the protected Map<String, String> getParams() method to add request parameters.

Guess you like

Origin blog.csdn.net/new_Aiden/article/details/50910209