Ym-Android network framework Volley (final), androidvolley
If you have never used Volley, let's take a look at the Android network framework Volley (experience) and Android Network Framework Volley (practice) in this article to better understand this article.
This article focuses on Volley's selection and implementation in some details. It is worth learning and if Volley is better used.
1. Why does the Volley local cache sometimes fail to be cached?
The Cache must be supported by the server, cached, and configured with the Cache-Control header,
Volley needs to determine whether the cache has expired from the header information. If Volley has expired, it will retrieve data from the network again.
I used the packet capture tool to capture the returned header information that cannot be cached.
Header information that can be cached
2. if we write a network request framework by ourselves, will our internal implementation choose HttpURLConnection or HttpClient?
Let's use the source code to see how Volley chooses to use it.
public static RequestQueue newRequestQueue(Context context, HttpStack stack) { File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR); String userAgent = "volley/0"; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); userAgent = packageName + "/" + info.versionCode; } catch (NameNotFoundException e) { } if (stack == null) { if (Build.VERSION.SDK_INT >= 9) { stack = new HurlStack(); } else { stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); } } Network network = new BasicNetwork(stack); RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network); queue.start(); return queue;}
If the mobile phone system version is later than 9 (Android 2.3), a HurlStack instance is created. Otherwise, an HttpClientStack instance is created. In fact, HurlStack uses HttpURLConnection for network communication, while HttpClientStack uses HttpClient for network communication. Why? Reference: Does Android use HttpURLConnection or HttpClient to access the network? That's why it's so fast.
From this point, we can learn that we should provide better processing methods for different SDK versions to achieve the best results.
3. Volley provides ImageRrequest, ImageLoader, and NetworkImageView for us. What scenarios are they used for? Why?
You can initiate an ImageReuqst request to load a single image. However, we recommend that you use ImageLoader to cache the application memory.
NetwoekImageView is used for batch image loading:
Public class NetworkImageView extends ImageView {private String mUrl; // The default image private int mDefaultImageId; // The image private int mErrorImageId displayed when loading fails; // public void setImageUrl (String url, ImageLoader imageLoader) {mUrl = url; mImageLoader = imageLoader; // This method will determine whether the ImageView size is valid and whether it is the same image. // before executing a new request, this will also cancel another invalid request initiated in the previous View. // Due to space limitations and too many lines of code, the specific implementation code loadImageIfNecessary (fals E) ;}// if the image has been moved off the screen and becomes invisible, the cancelling request @ Override protected void onDetachedFromWindow () {if (mImageContainer! = Null) mImageContainer. cancelRequest (); super. onDetachedFromWindow ();}}
When a ListView loads multiple images, NetworkImageView can prevent image errors and cancel the image loading request when the NetworkImageView slides out of the screen, this ensures the smoothness of the sliding list when loading multiple images. It provides a better user experience.
Can the Volley framework of Android be used for communication with WebService?
No, you also know that velloy uses the http protocol. Webservice uses soap. Two different communication mechanisms
YMCALL for Android phones requires traffic
Ymcall has very low traffic during the network call process. However, if you call and use ymcall in a Wi-Fi environment, you will enjoy a better experience.