Content
- What is the Volley database?
- What can Volley do?
- Volley Architecture
- Demonstrate how the Volley library obtains JSON data through the network
- References
For Android network operations, HttpClient and HttpConnection packages are generally introduced. The former is the Apache open-source library, and the latter is the Android built-in API. Enterprise applications generally use encapsulated http frameworks. Volley, android-async-http, fit, okhttp, androidquery, and AndroidAsync are popular. They have their own characteristics and different frameworks have different efficiency. Now Google has launched the official network communication library Volley for the Android platform, which enables faster, simpler, and more robust network communication, volley provides high-performance network communication functions, as well as good support for network image loading, which can fully meet the needs of simple REST clients. In addition, Volley is highly scalable. You can customize your network requests as needed.
What is the Volley database?
Volley is a database released by Ficus Kirpatrick at Gooogle I/O 2013 to process and cache network requests. This allows faster, simpler, and more robust network communication. The origin of Volley is a burst or emission of volume things or a large amount at once. In a Gooogle I/O 2013 speech, the image is a bow and arrow that emits fire. 1.
Figure 1 Gooogle I/O 2013 Volley
Volley manages processing and caching network requests, saving developers valuable time and writing the same network call/cache code again and again. Less code duplication, one of the advantages of which is to reduce bugs, which is expected by all developers.
The so-called "writing the same network call/cache code again and again" is the logic/code displayed by AsyncTask and the Web API you have compiled to obtain the response. You must take care of ProgressBar/ProgressDialog in the ProgressBar () and onPostExecute () methods. Although this is not an arduous task, it is boring and even bored, although I have defined the BaseTask class to manage the display/cancel of ProgressBar/ProgressDialog, and more other operations. Volley is a powerful tool to replace AsyncTask.
What can Volley do?
Volley is suitable for small and fast data transmission, especially in the following two scenarios:
- JSON object
- Image loading
Volley advantages:
Volley Architecture
Volley uses the thread pool as the basic structure, mainly divided into the main thread, cache thread and network thread. The main thread and cache thread have only one thread, while the NetworkDispatcher thread can have multiple threads, which can solve parallel problems.
Figure 2 Volley Architecture
Demonstrate how the Volley library obtains JSON data through the network
Download the Demo, import the Volley library and Demo project to eclipse, and then add the Volley Library to the Demo project. The program running result 3 is as follows:
Figure 3 demonstrate how the Volley library obtains JSON data through the network
Access the JSON interface provided by the National Meteorological Administration through Volley to obtain the weather conditions in Beijing.
- Http://www.weather.com.cn/data/sk/101010100.html
- Http://www.weather.com.cn/data/cityinfo/101010100.html
- Http://m.weather.com.cn/data/101010100.html
Where,101010100Represents Beijing.
The activity_main.xml file is just a TextView control and a ProgreeBar control. The core code is as follows:
package com.example.volleybasicexample;
import org.json.JSONObject;
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.Volley;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.weather.com.cn/data/sk/101010100.html";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
txtDisplay.setText("Response => " + response.toString());
findViewById(R.id.progressBar1)
.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
}
}
References
- Android Async HTTP Clients: Volley vs Retrofit
- Android Volley
Download Demo