Skip to content

Commit 32ef413

Browse files
committed
Added basic async download code. Throwing error because permissions needed first.
1 parent 1428252 commit 32ef413

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Top10Downloader/app/src/main/java/com/quockworks/top10downloader/MainActivity.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package com.quockworks.top10downloader;
22

3+
import android.os.AsyncTask;
34
import android.os.Bundle;
45
import android.support.v7.app.AppCompatActivity;
56
import android.support.v7.widget.Toolbar;
7+
import android.util.Log;
68
import android.view.Menu;
79
import android.view.MenuItem;
810

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+
917
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";
1019

1120
@Override
1221
protected void onCreate(Bundle savedInstanceState) {
@@ -15,6 +24,9 @@ protected void onCreate(Bundle savedInstanceState) {
1524
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
1625
setSupportActionBar(toolbar);
1726

27+
DownloadData downloadData = new DownloadData();
28+
downloadData.execute(top10Url);
29+
1830
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
1931
// fab.setOnClickListener(new View.OnClickListener() {
2032
// @Override
@@ -46,4 +58,59 @@ public boolean onOptionsItemSelected(MenuItem item) {
4658

4759
return super.onOptionsItemSelected(item);
4860
}
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+
}
49116
}

0 commit comments

Comments
 (0)