gpt4 book ai didi

java - Android 使用 Ksoap 作为 AsyncTask 解析变量并获取返回值

转载 作者:行者123 更新时间:2023-11-30 02:41:20 26 4
gpt4 key购买 nike

我想为 Ksoap 使用 AsyncTask 作为线程。因为我的应用程序在 Android 3 或更高版本中不起作用,在 Android 2 中没有任何问题。我想开发下面的代码以将参数发送到 AsyncTask 类并从中获取值。

public class ReceivedSMS extends ListFragment implements AbsListView.OnScrollListener {

public List<ReceiveFields> rows;

private int prevVisibleItem;

private TSMS tsms;

private String username;

private String password;

public Long getLastID;

private boolean isFirstTime;

private Context context;

private DatabaseHandler db;

private SQLiteDatabase dbHelper;

private ViewReceivedSMSDetailes receiveListView;


public ReceivedSMS(Context context, String username, String password) {

this.username = username;

this.password = password;

this.context = context;

}

public ReceivedSMS(String username, String password, long start, long count, Context context) {

this.username = username;

this.password = password;

this.context = context;

tsms = new TSMS(context, new User(this.username, this.password));

try {

getReceivedSMS(start, count);

} catch (Exception e1) {

e1.printStackTrace();

Log.e("Error in getReceivedSMS(start, count); ", "");
}

}

public List<ReceiveFields> getReceivedSMS(long start, long count) throws UnsupportedEncodingException {

tsms = new TSMS(context, new User(this.username, this.password));

try {

rows = tsms.getReceivedSMS(start, count);

saveRowsintoDatabase( rows );

} catch (TException e) {

e.printStackTrace();

Log.e(getClass().toString(), "ERROR IN Fetch SMS From WebService List<ReceiveFields> getReceivedSMS(long start, long count) "+ String.valueOf(e));

}

return rows;
}


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

db = new DatabaseHandler(context);

dbHelper = db.getWritableDatabase();

setReceivedSMSToListView();

}

}

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {

}

@Override
protected void onPostExecute(Void result) {

}

@Override
protected void onPreExecute() {

}

@Override
protected void onProgressUpdate(Void... values) {

}

}

此代码没有问题,但我想将操作从构造函数移至 AsyncCallWS,例如:

1) 在这个构造函数中:

public ReceivedSMS(String username, String password, long start, long count, Context context) {

this.username = username;

this.password = password;

this.context = context;

tsms = new TSMS(context, new User(this.username, this.password));

try {

getReceivedSMS(start, count);

} catch (Exception e1) {

e1.printStackTrace();

Log.e("Error in getReceivedSMS(start, count); ", "");
}

}

我要搬家:

tsms = new TSMS(context, new User(this.username, this.password));

try {

getReceivedSMS(start, count);

} catch (Exception e1) {

e1.printStackTrace();

Log.e("Error in getReceivedSMS(start, count); ", "");
}

AsyncCallWS 类和这个构造函数:

public List<ReceiveFields> getReceivedSMS(long start, long count) throws UnsupportedEncodingException {

tsms = new TSMS(context, new User(this.username, this.password));

try {

rows = tsms.getReceivedSMS(start, count);

saveRowsintoDatabase( rows );

} catch (TException e) {

e.printStackTrace();

Log.e(getClass().toString(), "ERROR IN Fetch SMS From WebService "+ String.valueOf(e));

}

return rows;
}

可以从 AsyncCallWS 类中获取 rowsAsyncCallWS 类。

更新帖子:

在这个类中doInBackground函数不允许返回String

public class WSDLHelper {
public static String call(SoapObject request){
ProcessTask p =new ProcessTask(request);
return p.execute();
}
}
class ProcessTask extends AsyncTask<Void, Void, SoapObject > {
SoapObject request;

public String ProcessTask(SoapObject rq){

request = rq;

}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(Void... params) {
String result = null;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);

AndroidHttpTransport transport = new AndroidHttpTransport(Strings.URL_TSMS);
transport.debug = true;

try {
transport.call(Strings.URL_TSMS + request.getName(), envelope);
result = envelope.getResponse().toString();
} catch (IOException ex) {
Log.e("" , ex.getMessage());
} catch (XmlPullParserException ex) {
Log.e("" , ex.getMessage());
}

if (result.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE)))
return result;
else
return null;
}

@Override
protected void onPostExecute(String result) {

super.onPostExecute(result);
}

}

最佳答案

您必须创建一个 AsyncTask首先。

public class ProcessTask extends AsyncTask<Void, Integer, String>{
String s1, s2, s3, s4;

public ProcessTask(String str1, String str2, String str3, String str4) {
// TODO Auto-generated constructor stub
s1 = str1;
s2 = str2;
s3 = str3;
s4 = str4;
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
//do something with strings
super.onPreExecute();
}

@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub

//your code of parsing

return null;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub

super.onPostExecute(result);
}
}

称它为:

ProcessTask p = new ProcessTask(s1, s2, s3, s4);
p.execute();

希望这对您有所帮助。

返回 List<ReceiveFields> , 变化:

public class ProcessTask extends AsyncTask<Void, Integer, String>

public class ProcessTask extends AsyncTask<Void, Integer, List<ReceiveFields>>

您需要删除现有的返回 String 的覆盖方法并覆盖返回 List 的正确方法 doInBackground。

关于java - Android 使用 Ksoap 作为 AsyncTask 解析变量并获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718315/

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