• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to show a progress bar on HTTP POST using asynctask?

danjuma9

Lurker
Hello android Forum brethren. 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? Also, while were at it, do you see anything wrong with my pattern.compile for my edit boxes?


Code:
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.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{
        //make sure the edit boxes have some information before proceeding
        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);
                dialog.dismiss();
                Alert.setMessage("Upload Succeeded");
                Alert.show();
                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!
 
I know this question is old but here goes my suggestion in case anyone ever needs.

You shouldn't be accessing the EditText from inside the thread (doInBackground method). Try passing the string directly to the thread via parameters.

Also you're not calling publish progress which would allow you to update the progress safely.

Check out this link here Droid Coders: Android: Modifying UI elements from inside a Thread for more details.

I hope it helps.
 
Back
Top Bottom