【发布时间】:2023-03-09 10:50:01
【问题描述】:
我想将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函数中不允许返回字符串
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);
}
}
【问题讨论】:
-
@Dhruti 我无法将操作从构造函数移动到
AsyncCallWS类并从中获取值。 -
如果你想返回字符串然后把
class ProcessTask extends AsyncTask<Void, Void, SoapObject >改成class ProcessTask extends AsyncTask<Void, Void, String >。 -
关于“UPDATE POST”,这是基于Dhruti错误的调用String result = p.execute();的提议,请参考已回答的后续问题这里:stackoverflow.com/questions/25758236/…
标签: java android android-asynctask