Android使用Ksoap作为具有解析变量的AsyncTask并获取返回值

时间:2014-09-08 06:11:58

标签: java android android-asynctask

我想将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;
}

可以从rows课程获得AsyncCallWSAsyncCallWS课程。

更新帖子:

此类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);
    }

}

1 个答案:

答案 0 :(得分:1)

您必须先创建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>>

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