开发者

How to show a progress bar on HTTP POST?

I am trying to upload a video to an api and I was wondering how you show a progress bar show and also dismiss it when an upload has finished?

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 r开发者_JS百科esult failed");
                Alert.show();
                return;
            }
            else
            {

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


    }

But it doesn't work. When i click on the button to send, it shows the handler for 2 seconds then brings up an error close.

Error log tells me that i have my errors caused at the

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

@ "do in background"

and somewhere in my onPrexecute.

What am I doing wrong, help please!


Just use AsyncTask. It was made specifically for such tasks: executing long-running code in the background and updating the UI properly (on UI thread).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜