So I'm trying to download 3 sheets from google docs. I use https://spreadsheets.google.com/tq?key=(link...) to get the txt file. To download each file I use an AsynchTask class.
The app manages to download only the first file. After that, it just fails with a HTTP/1.1 302 Moved Temporarily message.
This ONLY happens when I use my OLD phone which has API 16 (4.1.2). It does not happen on newer ones or my emulator.
Here is my AsynchTask class code
public class DownloadSheet extends AsyncTask<String, Void, String>{
private AsyncResult callback;
public DownloadSheet(AsyncResult callback){
this.callback = callback;
}
@Override
protected String doInBackground(String... urls) {
try{
return downloadUrl(urls[0]);
}catch (IOException e){
return "Unable to download sheet.";
}
}
@Override
protected void onPostExecute(String result){
//removes the unnecessary parts from the response and construct a JSON
int start = result.indexOf("{", result.indexOf("{") + 1);
int end = result.lastIndexOf("}");
Log.d("Ints: ",start +" "+ end);
Log.d("Result",result);
String jsonResponse = result.substring(start, end);
try {
JSONObject table = new JSONObject(jsonResponse);
callback.onResult(table);
}catch (JSONException e){
e.printStackTrace();
}
}
private String downloadUrl(String urlString) throws IOException {
InputStream is = null;
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(60000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int responseCode = conn.getResponseCode();
is = conn.getInputStream();
String contentAsString = convertStreamToString(is);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
private String convertStreamToString(InputStream is){
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try{
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
is.close();
}catch (IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
}
Here is my MainActivity where I call the class (I call this method on onCreate)
private void downloadSheet(){
new DownloadSheet(new AsyncResult() {
@Override
public void onResult(JSONObject object) {
mLeagues.add(processJsonLeague("England Championship",object));
}
}).execute("https://spreadsheets.google.com/tq?key=1a5bkkyEtY0xs-BmCLDrhntSneion_h5miqEDfsve-C4");
new DownloadSheet(new AsyncResult() {
@Override
public void onResult(JSONObject object) {
mLeagues.add(processJsonLeague("Premier League",object));
}
}).execute("https://spreadsheets.google.com/tq?key=1X5oGmyH4qbPQcGroR2RhwMX_RkoVYcmev4fLu9Rs9Ig");
new DownloadSheet(new AsyncResult() {
@Override
public void onResult(JSONObject object) {
mLeagues.add(processJsonLeague("England League 1",object));
}
}).execute("https://spreadsheets.google.com/tq?key=1hRiDvdLPkQEdTSVxmWEoWXjmCFQodNjMNYi3Fd7yYn0");
Log.d("Length",mLeagues.size() + "");
}
processJsonLeague method code
private League processJsonLeague(String leagueName,JSONObject object) {
ArrayList<Team> teams = new ArrayList<>();
try {
JSONArray rows = object.getJSONArray("rows");
for (int r = 0; r < rows.length(); ++r) {
JSONObject row = rows.getJSONObject(r);
JSONArray columns = row.getJSONArray("c");
String name = columns.getJSONObject(0).getString("v");
double hGoalAv = columns.getJSONObject(1).getDouble("v") / 4;
double aGoalAv = columns.getJSONObject(2).getDouble("v") / 4;
teams.add(new Team(name, hGoalAv, aGoalAv));
Log.d("Team", name + " " + hGoalAv + " " + aGoalAv);
}
} catch (JSONException e) {
e.printStackTrace();
}
return new League(leagueName, teams);
}
So that's it. What confuses me is that this problem occurs on my old device only.
Here is the result of my downloadUrl() method
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Thu, 13 Oct 2016 07:06:18 GMT
Location: https://docs.google.com/spreadsheets/d/1X5oGmyH4qbPQcGroR2RhwMX_RkoVYcmev4fLu9Rs9Ig/gviz/tq
P3P: CP="This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info."
P3P: CP="This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info."
Content-Encoding: gzip
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Server: GSE
Set-Cookie: NID=88=VmfjKSOqoHkZboIam76WYvlMIpaiTwKSh5sdHZiAstu8fcYs2OXQZOkRQXckYS1ZHiaU-hOlIKUm2bAxfv82dz7ACi6jN0wIJzJm6g5ZAuBTwjunKGwoESg-SufmHiwF;Domain=.google.com;Path=/;Expires=Fri, 14-Apr-2017 07:06:18 GMT;HttpOnly
Set-Cookie: NID=88=mMkSgEkEYA61zJPS_i2hSHO2oGL8sXgg-mcCwXW1mEbyMx912i1iEWfAXoXqNNYbyN0T_AgKNaPklEWYhqkENhMBFVUacp9yEBdu8HZc6uqbTRjI5RreSUIa39_23kg1;Domain=.google.com;Path=/;Expires=Fri, 14-Apr-2017 07:06:18 GMT;HttpOnly
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32"
Transfer-Encoding: chunked
00000001
00000001
�
00000001
00000001
��
00000001
��
00000001
��
00000001
��
00000001
��
00000001
��
00000001
��
00000001
m
00000001
�
000001
�
db
N�0D��
�ܳ�*�"7RڦI�D�B� �d�+j��nP�z\Ñ��vv4�[V��L"Vd���e�TzT�6�9��~�����:��e�W�nD����� z�����HH$��.
?��%*�XJ
��VT:7�G������p¸�
�h��Dtz�5:W�bq~{z�r��˯�y����)���e��r?�0��i"� �4�=�
%��O���,��%����
0