gpt4 book ai didi

java - 如何使用 AsyncTask 处理同一类中对 Web 服务的不同方法调用?

转载 作者:行者123 更新时间:2023-12-01 11:56:05 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它应该连接到 Web 服务并将数据保存到应用程序的本地数据库中。我使用 AsyncTask 从“Login”类连接到所述 Web 服务,然后将结果返回到“Login”中的 processFinish 方法。问题是我需要分离我在Web服务中的几个方法中引入的数据,并且据我所知,结果总是由processFinish处理。这是一个问题,因为我需要根据我调用的方法以不同的方式处理数据。

有没有办法告诉 processFinish 我调用了 Web 服务中的哪个方法,以便它可以以不同的方式处理结果?我考虑过从 Web 服务本身发送方法名称作为结果的一部分,但感觉很强制,而且我希望以更简洁的方式做到这一点。

这是我正在使用的代码:

LoginActivity.java(缩写):

public class LoginActivity extends ActionBarActivity implements AsyncResponse {

public static DBProvider oDB;
public JSONObject jsonObj;

public JSONObject jsonUser;

WebService webService;

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

oDB = new DBProvider(this);
}
/*Function that validates user and password (on button press).*/
public void validateLogin(View view){

ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

if (!isConnected){
Toast.makeText(this, "No connection!", Toast.LENGTH_LONG).show();
}else{

/* params contains the method's name and parameters I send to the web service. */
String[][][] params = {
{
{"MyWebServiceMethod"}
},
{
{"user", "myUserName", "string"},
{"pass", "myPass", "string"}
}
};

try{
webService = new WebService();
webService.delegate = this;
webService.execute(params);
}catch(Exception ex){
Toast.makeText(this, "Error!: " + ex.getMessage(), Toast.LENGTH_LONG).show();
}
}

}

public void processFinish(String result){
try{
// Here I handle the data in "result"

}
}catch(JSONException ex){
Toast.makeText(this, "JSONException: " + ex.getMessage(), Toast.LENGTH_LONG).show();
}
}

}

WebService.java:

public class WebService extends AsyncTask<String[][][], Void, String> {

/**
* Variable Declaration................
*
*/
public AsyncResponse delegate = null;
String namespace = "http://example.net/";
private String url = "http://example.net/WS/MyWebService.asmx";

public String result;

String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
HttpTransportSE HttpTransport;

/**
* Set Envelope
*/
protected void SetEnvelope() {

try {
// Creating SOAP envelope
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

//You can comment that line if your web service is not .NET one.
envelope.dotNet = true;

envelope.setOutputSoapObject(request);
HttpTransport = new HttpTransportSE(url);
HttpTransport.debug = true;

} catch (Exception e) {
System.out.println("Soap Exception---->>>" + e.toString());
}
}

@Override
protected String doInBackground(String[][][]... params) {
// TODO Auto-generated method stub
try {
String[][][] data = params[0];

final String methodName = data[0][0][0];
final String[][] arguments = data[1];

SOAP_ACTION = namespace + methodName;

//Adding values to request object
request = new SoapObject(namespace, methodName);

PropertyInfo property;

for(int i=0; i<arguments.length; i++){
property = new PropertyInfo();
property.setName(arguments[i][0]);
property.setValue(arguments[i][1]);

if(arguments[i][2].equals("int")){
property.setType(int.class);
}
if(arguments[i][2].equals("string")){
property.setType(String.class);
}
request.addProperty(property);
}

SetEnvelope();

try {
//SOAP calling webservice
HttpTransport.call(SOAP_ACTION, envelope);

//Got Webservice response
result = envelope.getResponse().toString();

} catch (Exception e) {
// TODO: handle exception
result = "Catch1: " + e.toString() + ": " + e.getMessage();
}
} catch (Exception e) {
// TODO: handle exception
result = "Catch2: " + e.toString();
}

return result;
}

@Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
/************************************/
}

AsyncResponse.java:

public interface AsyncResponse {
void processFinish(String output);
}

最佳答案

您可以使用不同的AsyncResponse类。匿名类使这更方便:

// in one place
webService.delegate = new AsyncResponse() {
@Override
void processFinish(String response) {
// do something
}
};

// in another place
webService.delegate = new AsyncResponse() {
@Override
void processFinish(String response) {
// do something else
}
};

关于java - 如何使用 AsyncTask 处理同一类中对 Web 服务的不同方法调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440563/

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