Android的,的AsyncTask与kSoap2 [英] Android, AsyncTask with kSoap2

查看:137
本文介绍了Android的,的AsyncTask与kSoap2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码主要使用从Web服务得到数据的应用程序,我想使用的AsyncTask运行在后台的SOAP调用......我是相当新的安卓(即在iOS程序员)所以我有点在这个新的...

I'm coding an app that primarily uses data gotten from a web service, and I want to use AsyncTask to run the SOAP calls in the background... I'm fairly new to Android(being an iOS programmer), so I'm a bit new at this...

现在,我有一个登录界面,在这里我把用户提供的登录名和检查对信息的服务器上...

Now, I have a login screen, where I take a user-provided login and check it against information on a server...

因此​​,在我登录活动:

So in my login activity:

    loginBtn.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            //Run the connection to authenticate the user
            AuthenticateConnection mAuth = new AuthenticateConnection();

            mAuth.mNumber = number;
            mAuth.mPassword = pass;

            mAuth.connection();
        }
    }

和我的皂类是这样的:

public class AuthenticateConnection
{
    private static final String SOAP_ACTION = "http://tempuri.org/Authenticate";
    private static final String METHOD_NAME = "Authenticate";
    private static final String NAMESPACE = "http://tempuri.org/";
    private String URL;

    public Boolean userOK;

    public String mNumber;
    public String mPassword;

    public AuthenticateConnection()
    {

    }

    public void connection()
    {
        Singleton service = Singleton.getInstance();
        String firstURL = service.getURL();
        URL = firstURL + "Parent.svc";

        System.out.println("Connection to: " + URL);

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

        //Add parameters
        request.addProperty("login", mNumber);
        request.addProperty("password", mPassword);

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.dotNet=true;
        envelope.implicitTypes=true;
        envelope.setAddAdornments(false);

        //Prepare request
        envelope.setOutputSoapObject(request);

        //Needed to make the internet call
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        //Allow for debugging - needed to output the request
        androidHttpTransport.debug = true;

        try 
        {
            //this is the actual part that will call the web service
            androidHttpTransport.call(SOAP_ACTION, envelope);

            //Get the SoapResult from the envelope body.
            //Object result = envelope.getResponse();
            //Object result = envelope.bodyIn;
            SoapObject sResult = (SoapObject)envelope.bodyIn;

            String tempID = sResult.getProperty("AuthenticateResult").toString();

            //Check if the user exists and has the correct password
            if(tempID != "-1")
            {
                userOK = true;

                //Store the values in the singleton class
                service.parentID = sResult.getProperty("AuthenticateResult").toString();
                service.parentToken = sResult.getProperty("token").toString();
            }

            //If -1 is returned, then either the number or the password is incorrect
            else
            {
                userOK = false;
            }           
        } catch(org.xmlpull.v1.XmlPullParserException ex2)
        {               
            //System.out.println(androidHttpTransport.requestDump.toString());

        }  catch (Exception e)
        {
            e.printStackTrace();
            System.out.println(androidHttpTransport.requestDump.toString());
        }
    }
}

所以我的问题是,我将如何做到这一点与AsyncTask的? 我一直在看有关AsyncTask的一些教程,但还没有真正得到它迄今为止...

So my question is, how would I do this with AsyncTask? I've been looking at some tutorial on AsyncTask, but haven't really "gotten it" so far...

推荐答案

您可以做的:

private class ConnectionTask extends AsyncTask<String, Void, Void> {
    private ProgressDialog dialog = new ProgressDialog(ACTIVITY_NAME.this);

    protected void onPreExecute() {
        dialog.setMessage("Connecting...");
        dialog.show();
    }

    protected void doInBackground(String... args) {
        AuthenticateConnection mAuth = new AuthenticateConnection();
        mAuth.mNumber = args[0];
        mAuth.mPassword = args[1];
        mAuth.connection();
    }

    protected void onPostExecute(Void v) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

然后调用它:

And then call it:

loginBtn.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        //Run the connection to authenticate the user
        new ConnectionTask().execute(number, pass);
    }
}

连接方法 AuthenticateConnection 应该返回的东西,以确保用户已通过验证。然后你就可以使用该值的 onPostExecute ,是这样的:

Your connection method in AuthenticateConnection should return something to ensure the user has been authenticated. Then you can use that value in the onPostExecute, something like this:

    protected void onPostExecute(Integer res) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (res.intValue() == OK) {
            /* Maybe start a new Activity ...*/
        } else {
            /* Maybe show a Toast with an error message ...*/
        }
    }

在这种情况下,的AsyncTask的签名将改变: 私有类ConnectionTask扩展的AsyncTask&LT;字符串,太虚,整数GT; 和doInBackground应返回一个整数

In this case the signature of the asynctask will change: private class ConnectionTask extends AsyncTask<String, Void, Integer> and the doInBackground should return an Integer.

希望它帮助。

这篇关于Android的,的AsyncTask与kSoap2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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