Googleドキュメントから3枚をダウンロードしようとしています。 私はhttps://spreadsheets.google.com/tq?key=(link...)を使ってtxtファイルを入手します。 各ファイルをダウンロードするには、AsynchTaskクラスを使用します。複数回呼び出すとAsyncTaskが失敗する(古いバージョンのAndroidでのみ発生する)
アプリは最初のファイルのみをダウンロードします。その後、HTTP/1.1 302 Moved Temporarilyメッセージで失敗します。
これはAPI 16(4.1.2)のOLD電話を使用している場合にのみ発生します。それは新しいものや私のエミュレータでは起こりません。ここで
は、ここで私は(私はのonCreateでこのメソッドを呼び出す)クラスを呼び出す私のMainActivityprivate 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メソッドのコード
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);
}
である私のAsynchTaskクラスコード
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();
}
}
です
それだけです。私が混乱しているのは、この問題が古いデバイスでのみ発生するということです。ここで
は私downloadUrl()メソッド
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
'2回以上呼び出すとAsyncTaskが失敗します。いいえ、それは悪い記述です。代わりに、同時に3つのタスクを開始しています。 Asynctasksはあなたの古いものでは異なって扱われます。あなたの古いものでは、彼らは本当に同時に実行されます。新しいアンドロイドにいる間、彼らは順番に実行されます。 – greenapps
ええ、確かに私は同時に3人を呼んでいます。私は明日、それが問題かどうかを確認するために他の人が終了した後、彼らに電話をかけてみるつもりです。もし誰かが何か提案を持っていれば、私はそれを試してみましょう。 – AresProductions