0

我想在等待来自 Web 服务请求的响应时显示进度条。但在此期间,android 进度条没有加载。

 public class WebService extends Activity {

          private static final String NAMESPACE="http://tempuri.org/";
          private static final String METHOD_NAME="AddEmployee";
          private static final String URL="http://10.32.4.24/Android/AndroidBus.svc";
          private static final String SOAP_ACTION="http://tempuri.org/IAndroidBus/AddEmployee";

          String celsius;
          Button b;
          TextView tv;
          EditText et;
          String res,resultval;

          @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_web_service);

             et=(EditText)findViewById(R.id.editText1);        
             tv=(TextView)findViewById(R.id.Result);
             b=(Button)findViewById(R.id.button1);
             b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                   new service().execute();
                }
          });
    }

    private class service extends AsyncTask<Void, Void, String> {
         ProgressDialog pd;
         protected void onPreExecute(){
             pd=new ProgressDialog(getBaseContext());
             pd.show();
         }
        @Override
        protected String doInBackground(Void... arg0) {
            System.out.println("In DoIn Background");

            // Initialize soap request + add parameters
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo pi=new PropertyInfo();
            pi.setName("Name");
            pi.setValue(et.getText().toString());
            request.addProperty(pi);

                        // Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            setProgress(1);

            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE( URL);

                // this is the actual part that will call the webservice
                androidHttpTransport.debug=true;
                androidHttpTransport.call(SOAP_ACTION, envelope);

                String resdump=androidHttpTransport.responseDump.toString();
                System.out.println(resdump);
                setProgress(2);
                // Get the SoapResult from the envelope body.
                //SoapObject result = (SoapObject) envelope.bodyIn;
                SoapPrimitive result=(SoapPrimitive)envelope.getResponse();
                setProgress(3);
                if (result != null) {
                    // Get the first property and change the label text
                    // txtFar.setText(result.getProperty(0).toString());
                    res = result.toString();
                } else {
                    Toast.makeText(getApplicationContext(), "No Response",
                            Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return res;

        }

        protected void onPostExecute(String h) {
            String result = h;
            pd.dismiss();
            tv.setText(result + "°F");

        }

    }


}

我想在发送和获取请求/响应时显示进度条。

4

5 回答 5

3

What you are trying to do is wrong. A progress set between SoapSerializationEnvelope, HttpTransportSE and Result ha no sense because huge work is made inside HttpTransportSE.call(...). If you wish a really download/upload progress bar on number of byte received and send, you have to modify HttpTrasportSE class. In detail, you have to modify call() and read() of this class.

As you can see here (HttpTransportSE.java), for example, for implementing upload progress, you have to create your HttpTransportSE, copy all original code and modify this portion:

public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile) throws IOException, XmlPullParserException {
(...)
OutputStream os = connection.openOutputStream();
os.write(requestData, 0, requestData.length);
os.flush();
os.close();
(...)

To this (i've considered common ProgressDialog):

public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile, ProgressDialog dialog)
    throws IOException, XmlPullParserException {
    (...)

    if(dialog != null)
        {
            dialog.setIndeterminate(false);
            dialog.setProgress(0);

            InputStream iss = new ByteArrayInputStream(requestData, 0, requestData.length);

            int byteCount = 0;
            byte[] buf = new byte[256];
            while (true) 
            {
                int rd = iss.read(buf, 0, 256);
                byteCount += rd;
                if (rd == -1)
                    break;
                os.write(buf, 0, rd);

                dialog.setProgress((int) (((float)byteCount/(float)requestData.length)*100));
            }
            dialog.setIndeterminate(true);

            iss.close();
            buf = null;
        }
        else
            os.write(requestData, 0, requestData.length);

        os.flush();
        os.close();
(...)

As you can see you have to override call method adding new parameter (ProgressDialog or ProgressBar-type according to your purposes) and passing it your dialog/bar (if you pass 'null', default logic is considered). This is the only way if you want progressbar shows real progress (by ksoap2).

For downloading, logic is the same. Find portion of code in which download is managed, save somewhere Content-Length value you can read from response header and implement similar upload "while" cycle for reading bytes considering content-length saved previously.

Hope this explanation can help you

于 2013-12-06T11:43:45.817 回答
0

四个步骤,我们开始..

(1)。清除活动中的进度对话框

private ProgressDialog dialog = null;

(2)。启动 AsyncTask 类时启动对话框

dialog = ProgressDialog.show(CurrentActivity.this, "", "loading..");

例如。

dialog = ProgressDialog.show(CurrentActivity.this, "", "loading search..");

SearchTask task= new GetSearchSeedsData();
task.execute(urlString);

(3)。在 AsyncTask 类的 doInBackground() 方法中完成繁重的工作(网络服务或任何)

(4)。然后在 AsyncTask 类的 onPostExecute() 中关闭进度对话框

dialog.dismiss();
于 2012-12-13T07:53:40.933 回答
0

尝试在进度对话框中设置消息..

protected void onPreExecute(){
         pd=new ProgressDialog(WebService.this);
         pd.setMessage("Loading...");
         pd.setIndeterminate(true);
         pd.show();
     }
于 2012-12-13T07:48:58.390 回答
0

你应该在这里尝试不同的上下文对象,

而不是这个,

 ProgressDialog(getBaseContext());

尝试

 ProgressDialog(ActivityName.this);
于 2012-12-13T07:49:01.837 回答
0

我不知道你的 setProgress 调用做了什么,但假设它更新了进度对话框,你必须让你的 asynctask 实现

 protected void onProgressUpdate(Integer... progress) {
     setProgress(progress);
 }

并打电话给publishProgress(2);doInBackground而不是setProgress

这是因为您无法更新在不同线程上运行的 doInBackGround 方法中的 ui 元素。这样做,您可能不仅不会更新对话框,还会破坏您的应用程序。

于 2012-12-13T07:49:45.360 回答