显示ProgressDialog上AsyncThread的Andr​​oid [英] Showing ProgressDialog on AsyncThread Android

查看:114
本文介绍了显示ProgressDialog上AsyncThread的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表明什么时候一个Web服务呼叫的呼叫是由ProgressDialog,这是我的code:

 公共类penAPIController扩展的AsyncTask<对象,无效对象> {私人浏览视图。
私人ProgressDialog对话框;公共penAPIController(视图V)
{
    鉴于= V;
}
    在preExecute保护无效()
    {
        this.dialog =新ProgressDialog(view.getContext());
        this.dialog.setMessage(载入中,请稍候......);
        this.dialog.setCancelable(假);
        this.dialog.show();
    }

该对话框显示doInBackground完成后,才真的,但我希望能够展示它,而doInBackground正在做的工作。然后隐藏它PostExecute

onPostExecute:

  @覆盖
        保护无效onPostExecute(obj对象)
        {
            //dialog.dismiss();
            myMethod的(OBJ);
        }    私有对象myMethod的(对象myvalue的)
    {
         //句柄值
         返回myvalue的;
    }

doInBackground:

  @覆盖
        保护对象doInBackground(对象...对象)
        {
            如果(objects.length< minNumberOfParams)
            {
                返回null;
            }
            对象finalObject = NULL;
            // TODO自动生成方法存根
            字符串NAMESPACE =HTTP:// ...;
            字符串METHOD_LOGIN_NAME =登陆;
            字符串SOAP_LOGIN_ACTION =HTTP:// ...;
            字符串METHOD_RUNACTION_NAME =RunAction;
            字符串SOAP_RUNACTION_ACTION =HTTP:// ...;
            string客户机=(字符串)对象[0]​​;
            字符串应用=(字符串)对象[1];
            字符串username =(字符串)对象[2];
            字符串密码=(字符串)对象[3​​];
            字符串的URL =(字符串)对象[4];
            串ACTION_NAME =(串)的对象[5];
            ArrayList的arrayParams = NULL;
            如果(objects.length ==(minNumberOfParams + 1))
            {
                arrayParams =(ArrayList的)对象[6]; //从ActionParam阵列构建XML参数
            }
            字符串参数= buildParametersXML(arrayParams);            SoapObject请求=新SoapObject空间(namespace,METHOD_LOGIN_NAME);            //客户
            的PropertyInfo propertyClient =新的PropertyInfo();
            propertyClient.setName(客户);
            propertyClient.setValue(客户端);
            propertyClient.setType(CLIENT.getClass());
            Request.addProperty(propertyClient);
            //应用
            的PropertyInfo propertyApplication =新的PropertyInfo();
            propertyApplication.setName(申请);
            propertyApplication.setValue(应用);
            propertyApplication.setType(APPLICATION.getClass());
            Request.addProperty(propertyApplication);
            //用户名
            的PropertyInfo propertyUsername =新的PropertyInfo();
            propertyUsername.setName(用户名);
            propertyUsername.setValue(用户名);
            propertyUsername.setType(USERNAME.getClass());
            Request.addProperty(propertyUsername);
            //密码
            的PropertyInfo propertyPassword =新的PropertyInfo();
            propertyPassword.setName(密码);
            propertyPassword.setValue(密码);
            propertyPassword.setType(PASSWORD.getClass());
            Request.addProperty(propertyPassword);            SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = TRUE;
            envelope.setOutputSoapObject(请求);            HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);
            尝试
            {
                androidHttpTransport.call(SOAP_LOGIN_ACTION,信封);
                SoapPrimitive响应=(SoapPrimitive)envelope.getResponse();
                字符串标记= response.toString();                SoapObject RequestRun =新SoapObject空间(namespace,METHOD_RUNACTION_NAME);                //令牌
                的PropertyInfo propertyToken =新的PropertyInfo();
                propertyToken.setName(令牌);
                propertyToken.setValue(标记);
                propertyToken.setType(token.getClass());
                RequestRun.addProperty(propertyToken);
                //操作名称
                的PropertyInfo propertyAction =新的PropertyInfo();
                propertyAction.setName(actionName);
                propertyAction.setValue(ACTION_NAME);
                propertyAction.setType(ACTION_NAME.getClass());
                RequestRun.addProperty(propertyAction);
                //参数
                的PropertyInfo propertyParams =新的PropertyInfo();
                propertyParams.setName(参数);
                propertyParams.setValue(参数);
                propertyParams.setType(PARAMETERS.getClass());
                RequestRun.addProperty(propertyParams);                SoapSerializationEnvelope envelopeRun =新SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelopeRun.dotNet = TRUE;
                envelopeRun.setOutputSoapObject(RequestRun);
                HttpTransportSE androidHttpTransportRun =新HttpTransportSE(URL);
                androidHttpTransportRun.call(SOAP_RUNACTION_ACTION,envelopeRun);
                SoapPrimitive responseRun =(SoapPrimitive)envelopeRun.getResponse();
                字符串结果= responseRun.toString();
                finalObject = parseOutputXML(结果);
            }
            赶上(例外五)
            {
                e.printStackTrace();
            }
            返回finalObject;
          }


解决方案

根据的评论,您呼叫的get()的AsyncTask 。这将阻止提交线程(在你的案件主UI线程),直到异步任务结果是可用的,即 doInBackground()的回报。

删除调用的get()键,例如处理完毕在 onPostExecute()或使用一个回调函数。

I want to show a ProgressDialog when a call to a Web Service call is made, this is my code:

public class penAPIController extends AsyncTask<Object, Void, Object>{

private View view;
private ProgressDialog dialog;

public penAPIController(View v)
{
    view = v;
}
    protected void onPreExecute() 
    {
        this.dialog = new ProgressDialog(view.getContext());
        this.dialog.setMessage("Loading, Please Wait..");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

The dialog shows indeed but only after doInBackground is finished, I want to be able to show it while doInBackground is doing its job. And then hide it on PostExecute

onPostExecute:

@Override
        protected void onPostExecute(Object obj)
        {
            //dialog.dismiss();
            myMethod(obj);
        }

    private Object myMethod(Object myValue)
    {
         //handle value 
         return myValue; 
    }

doInBackground:

@Override
        protected Object doInBackground(Object... objects)
        {
            if(objects.length < minNumberOfParams)
            {
                return null;
            }
            Object finalObject = null;
            // TODO Auto-generated method stub
            String NAMESPACE = "http://...";
            String METHOD_LOGIN_NAME = "Login";
            String SOAP_LOGIN_ACTION = "http://...";
            String METHOD_RUNACTION_NAME = "RunAction";
            String SOAP_RUNACTION_ACTION = "http://...";
            String CLIENT = (String)objects[0];
            String APPLICATION = (String)objects[1];
            String USERNAME = (String)objects[2];
            String PASSWORD = (String)objects[3];
            String URL = (String)objects[4];
            String ACTION_NAME = (String)objects[5];
            ArrayList arrayParams = null;
            if(objects.length == (minNumberOfParams + 1))
            {
                arrayParams = (ArrayList)objects[6];//Build parameters xml from ActionParam array
            }
            String PARAMETERS = buildParametersXML(arrayParams);

            SoapObject Request = new SoapObject(NAMESPACE, METHOD_LOGIN_NAME);

            //Client
            PropertyInfo propertyClient = new PropertyInfo();
            propertyClient.setName("client");
            propertyClient.setValue(CLIENT);
            propertyClient.setType(CLIENT.getClass());
            Request.addProperty(propertyClient);
            //Application
            PropertyInfo propertyApplication = new PropertyInfo();
            propertyApplication.setName("application");
            propertyApplication.setValue(APPLICATION);
            propertyApplication.setType(APPLICATION.getClass());
            Request.addProperty(propertyApplication);
            //Username
            PropertyInfo propertyUsername = new PropertyInfo();
            propertyUsername.setName("username");
            propertyUsername.setValue(USERNAME);
            propertyUsername.setType(USERNAME.getClass());
            Request.addProperty(propertyUsername);
            //Password
            PropertyInfo propertyPassword = new PropertyInfo();
            propertyPassword.setName("password");
            propertyPassword.setValue(PASSWORD);
            propertyPassword.setType(PASSWORD.getClass());
            Request.addProperty(propertyPassword);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(Request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try
            {
                androidHttpTransport.call(SOAP_LOGIN_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                String token = response.toString();

                SoapObject RequestRun = new SoapObject(NAMESPACE, METHOD_RUNACTION_NAME);

                //Token
                PropertyInfo propertyToken = new PropertyInfo();
                propertyToken.setName("token");
                propertyToken.setValue(token);
                propertyToken.setType(token.getClass());
                RequestRun.addProperty(propertyToken);
                //Action Name
                PropertyInfo propertyAction = new PropertyInfo();
                propertyAction.setName("actionName");
                propertyAction.setValue(ACTION_NAME);
                propertyAction.setType(ACTION_NAME.getClass());
                RequestRun.addProperty(propertyAction);
                //Parameters
                PropertyInfo propertyParams = new PropertyInfo();
                propertyParams.setName("parameters");
                propertyParams.setValue(PARAMETERS);
                propertyParams.setType(PARAMETERS.getClass());
                RequestRun.addProperty(propertyParams);

                SoapSerializationEnvelope envelopeRun = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelopeRun.dotNet = true;
                envelopeRun.setOutputSoapObject(RequestRun);
                HttpTransportSE androidHttpTransportRun = new HttpTransportSE(URL);
                androidHttpTransportRun.call(SOAP_RUNACTION_ACTION, envelopeRun);
                SoapPrimitive responseRun = (SoapPrimitive)envelopeRun.getResponse();
                String result = responseRun.toString();
                finalObject = parseOutputXML(result);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return finalObject;
          }

解决方案

Based on the comments, you are calling get() on the AsyncTask. This will block the submitting thread (main UI thread in your case) until the async task result is available, that is, doInBackground() returns.

Remove the call to get() and handle the completion e.g. in onPostExecute() or using a callback function.

这篇关于显示ProgressDialog上AsyncThread的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆