gpt4 book ai didi

Android,AsyncTask 与 kSoap2

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:01 26 4
gpt4 key购买 nike

我正在编写一个主要使用从 Web 服务获取的数据的应用程序,我想使用 AsyncTask 在后台运行 SOAP 调用...我对 Android 相当陌生(作为 iOS 程序员),所以我对此有点陌生...

现在,我有一个登录屏幕,我在其中获取用户提供的登录信息并根据服务器上的信息检查它...

所以在我的登录 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 的一些教程,但到目前为止还没有真正“理解”...

最佳答案

你可以这样做:

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

然后调用它:

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

你的 connection AuthenticateConnection 中的方法应该返回一些东西以确保用户已经过身份验证。然后您可以在 onPostExecute 中使用该值,像这样:

    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 的签名将会改变: private class ConnectionTask extends AsyncTask<String, Void, Integer>并且 doInBackground 应该返回 Integer .

希望对您有所帮助。

关于Android,AsyncTask 与 kSoap2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com