Android的使用KSOAP与解析变量的AsyncTask的并获得retured值 [英] Android use Ksoap as an AsyncTask with parse variables and get retured values

查看:154
本文介绍了Android的使用KSOAP与解析变量的AsyncTask的并获得retured值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的AsyncTask 为KSOAP作为Thread.because我的应用程序不的Andr​​oid 3.0或以上,并非Android的2我想任何问题,工作开发这个低于code到从发送参数的AsyncTask类,并获取值。

 公共类ReceivedSMS扩展ListFragment实现AbsListView.OnScrollListener {    公开名单< ReceiveFields>行;    私人诠释prevVisibleItem;    私人TSMS TSMS;    私人字符串用户名;    私人字符串密码;    众长getLastID;    私人布尔isFirstTime;    私人上下文的背景下;    私人数据库处理器分贝;    私人SQLiteDatabase dbHelper;    私人ViewReceivedSMSDetailes receiveListView;
    公共ReceivedSMS(上下文的背景下,字符串username,字符串密码){        this.username =用户名;        this.password =密码;        this.context =背景;    }    公共ReceivedSMS(字符串的用户名,密码字符串,长开始,长数,上下文的背景下){        this.username =用户名;        this.password =密码;        this.context =背景;        TSMS =新TSMS(背景下,新用户(this.username,this.password));        尝试{            getReceivedSMS(启动,计数);        }赶上(例外E1){            e1.printStackTrace();            Log.e(错误getReceivedSMS(启动,计数);,);
        }    }    公开名单< ReceiveFields> getReceivedSMS(长开始,长计数)抛出UnsupportedEncodingException {        TSMS =新TSMS(背景下,新用户(this.username,this.password));        尝试{            行= tsms.getReceivedSMS(启动,计数);            saveRowsintoDatabase(行);        }赶上(TException E){            e.printStackTrace();            Log.e(的getClass()的toString(),错误获取短信从WebService的名单,LT; ReceiveFields> getReceivedSMS(长开始,长数)+将String.valueOf(e)条);        }        返回行;
    }
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        DB =新数据库处理器(背景);        dbHelper = db.getWritableDatabase();        setReceivedSMSToListView();    }}私有类AsyncCallWS扩展的AsyncTask<太虚,太虚,太虚> {
    @覆盖
    保护无效doInBackground(虚空...... PARAMS){    }    @覆盖
    保护无效onPostExecute(虚空结果){    }    @覆盖
    在preExecute保护无效(){    }    @覆盖
    保护无效onProgressUpdate(虚空......值){    }}

这code不是问题,但我想移动从构造行动 AsyncCallWS ,例如:

1)在此构造:

 公共ReceivedSMS(字符串的用户名,密码字符串,长开始,长数,上下文的背景下){    this.username =用户名;    this.password =密码;    this.context =背景;    TSMS =新TSMS(背景下,新用户(this.username,this.password));    尝试{        getReceivedSMS(启动,计数);    }赶上(例外E1){        e1.printStackTrace();        Log.e(错误getReceivedSMS(启动,计数);,);
    }}

我想移动:

  TSMS =新TSMS(背景下,新用户(this.username,this.password));        尝试{            getReceivedSMS(启动,计数);        }赶上(例外E1){            e1.printStackTrace();            Log.e(错误getReceivedSMS(启动,计数);,);
        }

AsyncCallWS 类和这个构造:

 公开名单< ReceiveFields> getReceivedSMS(长开始,长计数)抛出UnsupportedEncodingException {    TSMS =新TSMS(背景下,新用户(this.username,this.password));    尝试{        行= tsms.getReceivedSMS(启动,计数);        saveRowsintoDatabase(行);    }赶上(TException E){        e.printStackTrace();        Log.e(的getClass()的toString(),错误获取短信从WebService的+将String.valueOf(e)条);    }    返回行;
}

可以获取 AsyncCallWS 类。 AsyncCallWS 类。

更新后的:

在这个类 doInBackground 函数不允许返回字符串

 公共类WSDLHelper {
    公共静态字符串调用(SoapObject要求){
        ProcessTask P =新ProcessTask(请求);
        返回p.execute();
    }
}
类ProcessTask扩展的AsyncTask<太虚,太虚,SoapObject> {
    SoapObject请求;    公共字符串ProcessTask(SoapObject RQ){        请求= RQ;    }    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
    }    @覆盖
    保护字符串doInBackground(虚空...... PARAMS){
        字符串结果= NULL;        SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(请求);        AndroidHttpTransport运输=新AndroidHttpTransport(Strings.URL_TSMS);
        transport.debug = TRUE;        尝试{
            transport.call(Strings.URL_TSMS + request.getName(),信封);
            。结果= envelope.getResponse()的toString();
        }赶上(IOException异常前){
            Log.e(,ex.getMessage());
        }赶上(XmlPullParserException前){
            Log.e(,ex.getMessage());
        }        如果(result.equals(将String.valueOf(整数,code_USER_PASS_FALSE)))
             返回结果;
        其他
            返回null;
    }    @覆盖
    保护无效onPostExecute(字符串结果){        super.onPostExecute(结果);
    }}


解决方案

您必须创建一个的 AsyncTask的第一。

 公共类ProcessTask扩展的AsyncTask<无效,整数,字符串> {
    串S1,S2,S3,S4;    公共ProcessTask(字符串STR1,字符串STR2,字符串STR3,字符串STR4){
        // TODO自动生成构造函数存根
        S1 = STR1;
        S2 = STR2;
        S3 = STR3;
        S4 = STR4;
    }    @覆盖
    在preExecute保护无效(){
        // TODO自动生成方法存根
        //做一些字符串
        super.on preExecute();
    }    @覆盖
    保护字符串doInBackground(虚空...... PARAMS){
        // TODO自动生成方法存根        //你解析code        返回null;
    }    @覆盖
    保护无效onPostExecute(字符串结果){
        // TODO自动生成方法存根        super.onPostExecute(结果);
    }
}

称其为:

  ProcessTask P =新ProcessTask(S1,S2,S3,S4);
p.execute();

希望这有助于。

要返回列表与LT; ReceiveFields> ,更改:

 公共类ProcessTask扩展的AsyncTask<无效,整数,字符串>

 公共类ProcessTask扩展的AsyncTask<无效,整数,列表< ReceiveFields>>

您需要删除它返回字符串现有的覆盖方法并重写的正确方法doInBackground,它返回列表。

I want to use AsyncTask for Ksoap as an Thread.because my app don't work in Android 3 or above and that not any problem in Android 2. I want to develop this below code to send parameters to AsyncTask class and get values from that.

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) {

    }

}

this code is not problem but i want to move action from constructors to AsyncCallWS such as :

1) in this constructor:

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); ", "");
    }

}

i want to move:

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); ", "");
        }

to AsyncCallWS class and this constructor:

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;
}

can be get rows from AsyncCallWS class. AsyncCallWS class.

UPDATE POST:

in this class doInBackground function do not allow to return 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);
    }

}

解决方案

You will have to create an AsyncTask first.

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

Call it as:

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

Hope this helps.

To return a List<ReceiveFields>, change:

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

to

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

You would need to remove existing overriden method which returns String and override the proper method doInBackground, which returns List.

这篇关于Android的使用KSOAP与解析变量的AsyncTask的并获得retured值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆