2015-10-27 31 views
-1

我使用class async以兩個活動下載和上傳數據。上傳不起作用,我不知道爲什麼。Android:AsyncTask和構造函數

這一個工作正常!

 new DatabaseConnector(true).execute("http://web2page.ch/apps/FruityNumber/highscoreShow.php"); 

但是這個不是!

 new DatabaseConnector(false).execute("http://web2page.ch/apps/FruityNumber/highscoreUpload.php?user=test7&highscore=timer7"); 

但是,如果我刪除「如果」在類中它工作正常......有人明白爲什麼嗎?

public DatabaseConnector(Boolean download) { 
    this.download = download; 
} 

@Override 
protected Long doInBackground(String... params) { 
    try { 
     try { 
      //Verbinden 
      url = new URL(params[0]); 
      httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.connect(); 

      if (download) { 
       //Falls die App nochmal geladen wird, sind die Daten nur einmal enthalten. Darum leeren. 
       arrayList.clear(); 

       inputStream = httpURLConnection.getInputStream(); 
       inputStreamReader = new InputStreamReader(inputStream); 
       bufferedReader = new BufferedReader(inputStreamReader); 

       for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) { 
        arrayList.add(line); 
       } 
      } else { 

      } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

回答

2

參見代碼中的註釋

new DatabaseConnector(false)意味着

public DatabaseConnector(Boolean download) { 
    this.download = false; // see here 
} 

所以

if (download) { // this won't execute because download == false 

else { 
    // there is nothing here to do! 
} 

我不知道你期望什麼不同。

解決這個問題將取決於你爲什麼甚至使用boolean變量。如果沒有理由,那就刪除它,不要傳遞任何東西。

否則,它聽起來就像你想把上傳的代碼在else{}

+0

正確的ORB,它怎麼有可能? – Blackbelt

+0

像那個bor @Blackbelt? – codeMagic

+0

絕對像那個bor – Blackbelt