Learning Andorid Development for some time, did a small application--watercress movie reader, as a summary of this stage of learning. The basic reader function is currently implemented, and more rich features are added later.
Code hosted on GitHub, Https://github.com/zhaoyu87/DouBan, needs classmates can download
The application is divided into two main pieces:
1. Data content, HTTP access watercress API to get movie information in JSON format
2. Display the interface, use a ListView to display the movie list, and a webview to display the movie details as follows:
1. Data content, HTTP access watercress API to get movie information in JSON format
The Watercress API documentation used in the app is shown in HTTP://DEVELOPERS.DOUBAN.COM/WIKI/?TITLE=API_V2.
The app uses the TOP250 movie API to get the movie list information api.douban.com/v2/movie/top250?count=20&start=0, which can be followed by the parameters count and start and Apikey
The JSON data that needs to be parsed are top250, simple subject, rating, and image, respectively, in the code shown in the following file
2. Display screen, use a ListView to display the movie list, and a webview to show the movie details
The display interface uses only one top250listactivity, the movie list and the movie details are implemented using Top250listfragment and moviedetailfragment two fragment respectively, The choice to use fragment instead of activity is primarily due to flexibility, allowing for later changes to the display, such as a view pager display, or a pad device support
The Application object plot is shown above
The entire application structure is not complex and is not explained in detail. The important points to be blunt:
1.top250listadapter in the Asynctask to create httpclient through the HttpGet access to the server to get a movie list, it is necessary to note that the HttpGet is set to the browser, otherwise the information can not be obtained normally
New defaulthttpclient (); New HttpGet (urls.base + Top250.append_url + "? count=" + Count + "&start=" + start + "&apikey=0537d7e598d847b20fa787b0c" a88d716 "); Httpget.setheader ("User-agent", "mozilla/5.0" (compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0) ");
2.top250listfragment to Moviedetailfragment jump process implementation: Top250listfragment defines the callback interface, This interface is implemented by Top250listactivity for fragment switching, and does not fragment switch directly in Top250listfragment, keeping two fragment Independent
Public Interface Callbacks { void ontop250itemclicked (simplesubject subject); }
3.MovieDetailFragment is created by the following Newinstance method, which facilitates the encapsulation of fragment
Public Static moviedetailfragment newinstance (String url) { new Bundle (); Bundle.putstring (Movie_detail_url, URL); New moviedetailfragment (); Fragment.setarguments (bundle); return fragment; }
4. Download the image using the Open source Framework Android-universal-image-loader (Https://github.com/nostra13/Android-Universal-Image-Loader), Support multi-threaded asynchronous load network, local picture, support image memory cache, file system cache. Recommend a blog http://blog.csdn.net/xiaanming/article/details/26810303
Note that if you need an SD card cache, you need to increase the permissions
<Manifest> <uses-permissionAndroid:name= "Android.permission.INTERNET" /> <!--Include Next permission if you want -allow UIL-to-cache images on SD card - <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE" /> ...</Manifest>
5.list view item experiences the Cardview,cardview inherited from the Framelayout class in Android 5.0 material design, which allows for consistent display in a card layout that can contain rounded corners and shadows.
List_item_top250.xml
<?XML version= "1.0" encoding= "Utf-8"?><Android.support.v7.widget.CardViewXmlns:app= "Http://schemas.android.com/apk/res-auto"xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/card_view"App:cardcornerradius= "8DP"Android:layout_width= "Match_parent"Android:layout_height= "120DP"> ......</Android.support.v7.widget.CardView>
To import the support library Android.support.v7.widget.CardView, you need to add it in Build.gradle dependencies, as follows
dependencies { compile filetree (include: [' *.jar '], dir: ' Libs ') ' Com.android.support: appcompat-v7:21.0.3 ' compile ' com.android.support:cardview-v7:21.+ ' compile files (' Libs/universal-image-loader-1.9.3.jar ')}
The above is a brief introduction to this small application, and share with you, learn together. If you find any bug or have suggestions welcome message
Open source Android Watercress movie reader