Source code analysis of Jamendo open-source online music player

Source: Internet
Author: User

Iv. Network Operations. Call the music API
The gallery on HomeActivity loads the most popular album this week, which is asynchronously loaded using NewsTask. The following describes the process of requesting network APIs in doInBackground () of NewsTask () medium:
[Java]
JamendoGet2Api server = new JamendoGet2ApiImpl ();
Album [] albums = null;
Albums = server. getPopularAlbumsWeek ();
The com. teleca. jamendo. api package is an interface package for calling music APIs.
JamendoGet2Api is the interface for calling music APIs. Because you do not need to authorize these APIs in any way, unlike calling interfaces of Sina Weibo, you must use oauth to authorize them, so as long as JamendoGet2Api service = new JamendoGet2ApiImpl (); then call the corresponding method and directly call the corresponding api through REST, we can see that each method in the interface has an explanation, the corresponding APIs are also written. JamendoGet2ApiImpl is the implementation of JamendoGet2Api. We track the getPopularAlbumsWeek () method that gets the most popular album this week and request it using the GET method, tracking the doGet () method in the Caller class. The http operation uses the Apache HTTP Client package, which uses a cache to cache the request results to reduce network overhead, let's talk about this later.
The specific http operations are not mentioned much. Let's just look at the code. The get method returns a json string and you can directly access this api in the browser: You can see the returned json string, android provides a method for processing json strings. We can see that the returned json string is a large JSONArray, and each item in it is a JSONObject, that is, the corresponding album information, getPopularAlbumsWeek () the json string is converted into an array of corresponding objects. Here is Album [], which sets the information in each JSONObject to the Album object class, mainly AlbumFunctions and AlbumBuilder.
The returned Album [] is used by the gallery adapter directly through the setList () method. Network operations are similar, and the steps for calling other APIs are similar.
The above is a specific process, asynchronously requesting an api, network operations return a json string, processing json into an object, and finally display it. This network encapsulation can also be used in your own code. The jamendo hierarchy is clear and well encapsulated.
By the way, the cache used by Caller. The following code gets the cache and stores it in the cache:
[Java]
/**
* Cache for most recent request
*/
Private static RequestCache requestCache = null;
 
String data = null;
If (requestCache! = Null ){
Data = requestCache. get (url );
If (data! = Null ){
Log. d (JamendoApplication. TAG, "Caller. doGet [cached]" + url );
Return data;
}
}
 
If (httpEntity! = Null ){
InputStream inputStream = httpEntity. getContent ();
Data = convertStreamToString (inputStream );
// Cache the result
If (requestCache! = Null ){
RequestCache. put (url, data );
}
 
}
Where does the program generate the RequestCache object? We found a static setRequestCache () method in the Caller class, indicating where this method was called, besides, it was called before, and we thought of the global application of JamendoApplication. This class is executed before the first activity is executed and has its own life cycle in its onCreate () the setRequestCache () method is found:
[Java]
MRequestCache = new RequestCache ();
Caller. setRequestCache (mRequestCache );
The following describes the cache class RequestCache.
[Java]
Private history list history;
Private Hashtable <String, String> cache;
Up to 10 latest data records can be cached. history is a queue used to store the request url. If there are more than 10 requests, the first poll will be taken out. Cache is a hashtable used to store the returned result json of the corresponding url. history is mainly used to assist the remove operation of the cache. If the url you want to query is already in the cache, it is extracted from the cache, saving the overhead of the network request again.
[Java]
Public void put (String url, String data ){
History. add (url );
// Too much in the cache, we need to clear something
If (history. size ()> CACHE_LIMIT ){
String old_url = (String) history. poll ();
Cache. remove (old_url );
}
Cache. put (url, data );
}
Public String get (String url ){
Return cache. get (url );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.