[英]Android use Ksoap as an AsyncTask with parse variables and get returned values
我想將AsyncTask
用於Ksoap作為線程。因為我的應用程序無法在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
類獲取rows
。 AsyncCallWS
類。
更新后:
在此類中, 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的重寫方法,並重寫適當的doInBackground方法,該方法返回List。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.