2011-09-26 24 views
1

我想將視頻上傳到api,我想知道如何顯示進度條顯示,並在上傳完成時解除它?如何在HTTP POST上顯示進度條?

public class Loadvid extends AsyncTask <Object,Integer,String>{ 
     EditText etxt_user = (EditText) findViewById(R.id.user_email); 
     EditText etxt_pass = (EditText) findViewById(R.id.friend_email); 

     protected void onPreExecute(){ 
      dialog = new ProgressDialog(share.this); 
      dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      dialog.setMax(100); 
      dialog.show(); 
      super.onPreExecute(); 

     } 

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

      if(etxt_user.equals("")||etxt_pass.equals("")){ 
       dialog.dismiss(); 
       Alert.setMessage("Please fill in the Blanks"); 
       Alert.show(); 

      } 
else{ 
     Pattern keys= Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + 
         "\\@" + 
         "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + 
         "(" + 
         "\\." + 
         "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + 
         ")+"); 
      Matcher matcher = keys.matcher((CharSequence) etxt_user); 
      Matcher matcher1 = keys.matcher((CharSequence) etxt_pass); 
      if(!matcher.matches()|| !matcher1.matches()) 
{ 
        dialog.dismiss(); 
        Alert.setMessage("Wrong email address."); 
        Alert.show(); 

       } 
      } 

      //implement the httppost. 
     try 
      { 

MultipartEntity me = new MultipartEntity(); 

me.addPart("file", new FileBody(new File("/"))); 

httppost.setEntity(me); 

HttpResponse responsePOST = client.execute(httppost); 
HttpEntity resEntity = responsePOST.getEntity(); 

InputStream inputstream = resEntity.getContent(); 
BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream)); 
StringBuilder stringbuilder = new StringBuilder(); 
String currentline = null; 

while ((currentline = buffered.readLine()) != null) 
    { 
    stringbuilder.append(currentline + "\n"); 
    String result = stringbuilder.toString(); 
    Log.v("HTTP UPLOAD REQUEST",result); 
    inputstream.close();} 
} 

    catch (Exception e) { 
    e.printStackTrace(); 

    } 


      return null; 
     } 


     protected void onPostExecute(String result){ 

      super.onPostExecute(result); 

      if(result==null){ 
       dialog.dismiss(); 
       Alert.setMessage("The result failed"); 
       Alert.show(); 
       return; 
      } 
      else 
      { 

       Intent intent = new Intent("com..."); 
       startActivity(intent); 
      } 
     } 


    } 

但它不起作用。當我點擊按鈕發送,它顯示處理程序2秒,然後提出一個錯誤關閉。

錯誤日誌告訴我,我必須在

Matcher matcher = keys.matcher((CharSequence) etxt_user); 

造成@我的錯誤「做背景」

和地方在我onPrexecute。

我在做什麼錯,請幫忙!

回答

3

只是使用AsyncTask.它是專門爲這些任務制定的:在後臺執行長時間運行的代碼並正確更新UI(在UI線程上)。

+0

感謝彼得,我已經嘗試使用異步任務,但它上傳失敗。我將用我擁有的新東西更新我的問題。 – user875139