2015-10-05 64 views
0

目標更新進度

我所試圖做的是,查詢我的價值觀傳遞給查詢我的數據庫,並將結果傳遞給我的列表視圖,但是ListView控件加載之前,我想有一個顯示項目下載進度的進度對話框。

我做了什麼

查詢我的數據庫,更新列表視圖之前顯示的進度對話框

問題

,我遇到的進展情況未被髮布的問題。和我的連接長度是-1。我相信這是我的問題

請參閱下面的代碼。儘管有閱讀,但我並不完全熟悉發佈更新,所以我希望能得到一些幫助。 謝謝

public class StudentAsynTask extends AsyncTask<String, Integer, Boolean> { 




@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressD = new ProgressDialog(retrievrSData.this); 
     progressD.setMessage("Retrieving Results"); 
     progressD.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressD.setIndeterminate(true); 
     progressD.setProgress(0); 
     progressD.show(); 

    } 

    @Override 
    protected Boolean doInBackground(String... params) { 

     try { 

      URL url = new URL(params[0]); 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // THIS IS -1 
      int fileLength = conection.getContentLength(); 


      HttpParams param = new BasicHttpParams(); 
      ArrayList<NameValuePair> inputArguments = new ArrayList<NameValuePair>(); 

      if (firstName.equals("''") && !lastname.equals("''")) { 
       inputArguments.add(new BasicNameValuePair("l_name", lastname)); 
      } else if (!firstName.equals("''") && lastname.equals("''")) { 
       inputArguments.add(new BasicNameValuePair("f_name", firstName)); 

      } else if (!firstName.equals("''") && !lastname.equals("''")) { 
       inputArguments.add(new BasicNameValuePair("f_name", firstName)); 
       inputArguments.add(new BasicNameValuePair("l_name", lastname)); 
      } 


      HttpClient client = new DefaultHttpClient(param); 
      HttpPost request = new HttpPost("http:// technologies.com/Das/getst.php"); 
      request.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
      request.setEntity(new UrlEncodedFormEntity(inputArguments, "UTF-8")); 
      HttpResponse httpResponse = (HttpResponse) client.execute(request); 


      int status = httpResponse.getStatusLine().getStatusCode(); 

      if (status == 404) { 
       Intent intent = new Intent(); 
       setResult(404, intent); 
       finish(); 
      } 
      if (status == 200) { 

       if (fileLength > 0) { 
        publishProgress((int) (5 * 100/fileLength)); 
       } 

       HttpEntity entity = httpResponse.getEntity(); 
       String data = EntityUtils.toString(entity); 

以下是進度更新代碼。

@Override 
    protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     // if we get here, length is known, now set indeterminate to false 
     progressD.setIndeterminate(false); 
     progressD.setMax(100); 
     progressD.setProgress(progress[0]); 
    } 
+0

內容長度不可用,因爲默認Android請求GZIP壓縮響應。 來源:[Android文檔](http://developer.android.com/reference/java/net/HttpURLConnection.html) –

+0

@MarkusKauppinen我無法獲得進度條的工作? – Niana

+0

你有-1值,你傳遞給onProgressUpdate(-1)。這是你的問題 。 按布爾值更改整數 –

回答

0

折騰這樣的:

doInBackground你必須檢查你的工作是否正確。 如果你做得好,那麼,你必須通過onProgressUpdate。如果沒有,你必須通過虛假onProgressUpdate。 onProgressUpdate的結構必須是這樣的:

onProgressUpdate(boolean flag) 

爲progressD的增加值(0,100),你必須做你的doInBackground方法。

if (status == 404) { 
       intValueOfProgress = 0; 
} 
if (status == 200) { 
       intValueOfProgress = 100; 
} 
+0

謝謝,但這並沒有說太多先生 – Niana