如何在HTTP POST上显示进度条?

时间:2011-09-26 21:04:59

标签: android multithreading http-post handler

我正在尝试将视频上传到api,我想知道如何显示进度条显示,并在上传完成后将其解除?

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中的某个地方。

我做错了什么,请帮忙!

1 个答案:

答案 0 :(得分:3)

只需使用AsyncTask.它专门用于此类任务:在后台执行长时间运行的代码并正确更新UI(在UI线程上)。