【问题标题】:Android use Ksoap as an AsyncTask with parse variables and get returned valuesAndroid 使用 Ksoap 作为 AsyncTask 解析变量并获取返回值
【发布时间】: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 类中获取rowsAsyncCallWS 班级。

更新帖子:

在这个类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&lt;Void, Void, SoapObject &gt;改成class ProcessTask extends AsyncTask&lt;Void, Void, String &gt;
  • 关于“UPDATE POST”,这是基于Dhruti错误的调用String result = p.execute();的提议,请参考已回答的后续问题这里:stackoverflow.com/questions/25758236/…

标签: java android android-asynctask


【解决方案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&lt;ReceiveFields&gt;,请更改:

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

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

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

【讨论】:

  • 感谢我的 doInBackground 必须返回 ReceiveFields 但这不允许它从 protected String doInBackground 更改为 protected List&lt;ReceiveFields&gt; doInBackground
  • 你能帮我更新帖子吗?
  • @MahdiPishguy 返回字符串使用class ProcessTask extends AsyncTask&lt;Void, Void, String &gt;
猜你喜欢
  • 1970-01-01
  • 2014-06-08
  • 2012-01-08
  • 1970-01-01
  • 2013-03-22
  • 2016-06-10
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
相关资源
最近更新 更多