繁体   English   中英

添加帖子后显示进度对话框

[英]Progress dialog showing after adding a post

我有以下代码,我想在进度对话框显示时执行(向数据库添加一行):

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();}

我已经尝试了AsyncTask和线程,但没有任何效果,有时它可以工作,但是在添加帖子后显示了进度对话框,请帮忙吗?

为此,您需要使用Mounzer所说的AyncTask。 要执行progressBar,您将必须上传一些数据,回来并更新您的progressBar,加载更多数据,等等。

幸运的是,AsyncTask使此操作变得容易。 这个非常简单的示例计数为1000,并在每次迭代时使用TextView发布结果。 您可以轻松地将其更改为progressBar并将您的http邮政编码放入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;


        }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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