1
1
package com .quockworks .top10downloader ;
2
2
3
+ import android .os .AsyncTask ;
3
4
import android .os .Bundle ;
4
5
import android .support .v7 .app .AppCompatActivity ;
5
6
import android .support .v7 .widget .Toolbar ;
7
+ import android .util .Log ;
6
8
import android .view .Menu ;
7
9
import android .view .MenuItem ;
8
10
11
+ import java .io .IOException ;
12
+ import java .io .InputStream ;
13
+ import java .io .InputStreamReader ;
14
+ import java .net .HttpURLConnection ;
15
+ import java .net .URL ;
16
+
9
17
public class MainActivity extends AppCompatActivity {
18
+ public static final String top10Url = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml" ;
10
19
11
20
@ Override
12
21
protected void onCreate (Bundle savedInstanceState ) {
@@ -15,6 +24,9 @@ protected void onCreate(Bundle savedInstanceState) {
15
24
Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
16
25
setSupportActionBar (toolbar );
17
26
27
+ DownloadData downloadData = new DownloadData ();
28
+ downloadData .execute (top10Url );
29
+
18
30
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
19
31
// fab.setOnClickListener(new View.OnClickListener() {
20
32
// @Override
@@ -46,4 +58,59 @@ public boolean onOptionsItemSelected(MenuItem item) {
46
58
47
59
return super .onOptionsItemSelected (item );
48
60
}
61
+
62
+ // 3 parameters: 1) download location, 2) progress bar, 3) resultant data type
63
+ private class DownloadData extends AsyncTask <String , Void , String >{
64
+
65
+ private String mFileContents ;
66
+
67
+ // Run without blocking
68
+ @ Override
69
+ protected String doInBackground (String ... params ) {
70
+ mFileContents = downloadXMLFile (params [0 ]);
71
+ if (mFileContents == null ){
72
+ Log .d ("DownloadData" , "Error downloading" );
73
+ }
74
+ return mFileContents ;
75
+ }
76
+
77
+ private String downloadXMLFile (String urlPath ){
78
+ StringBuilder tempBuffer = new StringBuilder ();
79
+ try {
80
+ // Try opening an http url connection
81
+ URL url = new URL (urlPath );
82
+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
83
+ int response = connection .getResponseCode ();
84
+ Log .d ("DownloadData" , "The response code was " + response );
85
+ InputStream is = connection .getInputStream ();
86
+ InputStreamReader isr = new InputStreamReader (is );
87
+
88
+ /* Download data */
89
+ int charRead ;
90
+ char [] inputBuffer = new char [500 ]; // Allocate space to read file 500 bytes at a time
91
+ while (true ){
92
+ charRead = isr .read (inputBuffer ); // try to read data into inputBuffer, charRead is the number of characters read
93
+ if (charRead <= 0 ){
94
+ break ;
95
+ }
96
+
97
+ // Store characters from buffer, append to the temp buffer
98
+ tempBuffer .append (String .copyValueOf (inputBuffer ),0 ,charRead );
99
+ }
100
+ return tempBuffer .toString ();
101
+
102
+ } catch (IOException e ){
103
+ Log .d ("DownloadData" , "IO Exception reading data: " + e .getMessage ());
104
+ } catch (SecurityException e ){
105
+ Log .d ("DownloadData" , "Security Exception. Needs permissions? " + e .getMessage ());
106
+ }
107
+ return null ;
108
+ }
109
+
110
+ @ Override
111
+ protected void onPostExecute (String result ) {
112
+ super .onPostExecute (result );
113
+ Log .d ("DownloadData" , "Result was " + result );
114
+ }
115
+ }
49
116
}
0 commit comments