从asynctask获得价值

时间:2017-07-13 09:46:32

标签: android android-asynctask

我找到了许多主题,但我无法获得解决方案,我在asyncBask的asInBackground方法中执行soap请求,我想让Integer知道进程是否完成,这里我称之为asyncTask:

            Simulation.AsyncSoapCall task = new Simulation.AsyncSoapCall();
            try {
                Integer taskResult = task.execute().get();
            } catch (Exception e) {
                e.printStackTrace();
            }

我的AsyncTask类:

private class AsyncSoapCall extends AsyncTask<Void, Void, Integer> {


    Integer result;
    Boolean isInternetPresent = false;
    Boolean isUrlAvailable = false;
    ConnectionDetector cd;

    AsyncSoapCall(){
    }

    @Override
    protected Integer doInBackground(Void... params) {

        cd = new ConnectionDetector(getActivity().getApplicationContext());
        // get Internet status
        isInternetPresent = cd.isConnectingToInternet();

        // check for Internet status
        if (isInternetPresent) {

            String namespace = getResources().getString(R.string.NAMESPACE);
            String url = getResources().getString(R.string.URL);
            String soapaction = getResources().getString(R.string.SOAP_ACTION);
            String login = getResources().getString(R.string.login);
            String mdp = getResources().getString(R.string.mdp);

            isUrlAvailable = cd.isUrlAvailable();

            // check for Internet status
            if (isUrlAvailable) {

                String idApplication = Installation.id(getActivity());

                SOAPContact soapContact = new SOAPContact(namespace, url, soapaction, login, mdp);

                soapContact.saveParams(getResources().getString(R.string.origine), db);

                result = 1;
            } else {
                result = 2;
            }
        } else {
            result = 3;
        }

        return result;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }
}

我不会在此行收到错误我的应用crasha:                     整数taskResult = task.execute()。get();

3 个答案:

答案 0 :(得分:0)

尝试从onPostExecute获取值,如

@Override
protected void onPostExecute(Integer result) {
    super.onPostExecute(result);
int yourNum = result;
}

它是

答案 1 :(得分:0)

你读过这份文件吗? https://developer.android.com/reference/android/os/AsyncTask.html

AsyncTask没有“get”方法。 您需要定义一个OnPostExecute方法,当您的任务结束时,将以Integer作为参数调用该方法。

public class MyActivity extends Activity
{
    private Integer myInteger;
    private void blabla(){
        Simulation.AsyncSoapCall task = new Simulation.AsyncSoapCall() {
            @Override
            protected void onPostExecute(Integer result) {
                //... Your code here ...
                   MyActivity.this.myInteger = result;
                   MyActivity.this.myMethod(result);   
            }
        }
        try {
            task.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void myMethod(Integer integer){
    }
}

答案 2 :(得分:0)

这是一种借助接口的方法,

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    static String TAG=MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AsyncSoapCall request = new AsyncSoapCall(new AsyncSoapCall.AsyncSoapInterface() {
            @Override
            public void callBack(String callBackValue) {

                Log.d(TAG,callBackValue);

            }
        });
        request.execute();


    }
}

<强> AsyncSoapCall.java

public class AsyncSoapCall extends AsyncTask<Void,Void,Void> {


    interface AsyncSoapInterface{


        void callBack(String callBackValue);
    }

    AsyncSoapInterface callbackObj;

    AsyncSoapCall(AsyncSoapInterface callbackObj)
    {
        callbackObj = callbackObj;


    }




    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        callbackObj.callBack("Your value");

    }
}
相关问题