简体   繁体   中英

Progress dialog showing after adding a post

i have the following code I want to execute (adding a row to database) it while a progress dialog is showing :

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.1.38/hu/Addpost.php?username="+username+"&fname="+firstname+"&lname="+lastname+"&dop="+now+"&content="+post3+"&type="+post_type2+"");

                try
                {

                    nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("FirstN",firstname));
                    nameValuePairs.add(new BasicNameValuePair("LastN",lastname));
                    nameValuePairs.add(new BasicNameValuePair("Content",post1));
                    nameValuePairs.add(new BasicNameValuePair("type",post_type));
                    nameValuePairs.add(new BasicNameValuePair("Dateofp",now));
                    nameValuePairs.add(new BasicNameValuePair("username",username));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    response = httpclient.execute(httppost);

                    if(response.getStatusLine().getStatusCode()==200)
                    {
                    entity=response.getEntity();
                    if(entity !=null)
                    {
                        Toast.makeText(getBaseContext(),"POST SUCCESFULLY ADDED",Toast.LENGTH_LONG).show(); 
                    }

                    }
                    else
                    {
                        Toast.makeText(getBaseContext(),"ERROR RERTY OR CHECK YOUR CONNECTION",Toast.LENGTH_LONG).show();
                    }





                    wholepost.setText("");





                }
                catch(Exception ex)
                {Toast.makeText(getBaseContext(),"CONNECTION ERROR",Toast.LENGTH_LONG).show();}

I've tried AsyncTask and threads but nothing worked,and sometimes it worked but the progress dialog was shown after the post was added, any help please ?

to do this right, you need to use AyncTask as Mounzer says. To do a progressBar, you're going to have to do some uploading of the data, come back and update your progressBar, load more data, etc.

Fortunately, AsyncTask makes that easy to do. This very simple example counts to 1000 and at each iteration publishes the results with a TextView. You could easily change that to a progressBar and put your http post code in doInBVackground()

protected class InitTask extends AsyncTask<Integer, String, Void> {


        // add the thread ID and count to the list
        @Override
        protected void  onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            TextView tv = new TextView(mContext);
            mThreadOutput.addView(tv);
            tv.setText(values[0]);

        }

        // do the thread work. at each iteration, set the progress to be
        // updated by the UI thread
        protected Void doInBackground(Integer... params ) {

            for (int i = 0; i < 1000; i++) {
                String s = "Thread " + params[0] + ": " + i; 
                publishProgress( s );
            }
            return null;


        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM