有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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和threads,但都没用,有时也能用,但添加帖子后会显示进度对话框,请问有什么帮助吗


共 (1) 个答案

  1. # 1 楼答案

    要做到这一点,你需要像蒙泽所说的那样使用AyncTask。要做progressBar,你需要上传一些数据,回来更新你的progressBar,加载更多数据,等等

    幸运的是,AsyncTask使这件事变得容易。这个非常简单的例子可以计算到1000 在每次迭代中,都会用文本视图发布结果。您可以轻松地将其更改为progressBar,并将http post代码放入DoinbVackRound()中

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