Select to view content in your preferred language

Geoprocessor and AsyncTask

3053
1
Jump to solution
03-04-2015 06:46 AM
ÅsaWegelius
Deactivated User

Hello, I am trying to use a Geoprocessor in an app to consume an aynchronous service. First I tried with Handler as in https://community.esri.com/thread/51073. But I could not figure out how to get it to work in a Fragment. So I decided to use AsyncTask instead. I get the right error message if I use wrong credentials and the job submits but then it stucks at jobstatus submitted.

This is the input parameters:

Parameter: VehicleID

Data Type: GPLong

Display Name: VehicleID

Description: VID

Direction: esriGPParameterDirectionInput

Default Value: 1

Parameter Type: esriGPParameterTypeRequired

Category:

Parameter: CalculationID

Data Type: GPLong

Display Name CalculationID

Description: CID

Direction: esriGPParameterDirectionInput

Default Value: 0

Parameter Type: esriGPParameterTypeRequired

Category:

This is how I start the job:

    public void sendJob(String username, String password, long id) throws Exception{

        UserCredentials creds = new UserCredentials();

        creds.setUserAccount(username, password);

        ArrayList<GPParameter> parameters = new ArrayList<GPParameter>();

        GPLong idParam = new GPLong("VehicleID");

        idParam.setValue(id);

        GPLong calcParam = new GPLong("CalculationID");

        calcParam.setValue(0);

        parameters.add(idParam);

        parameters.add(calcParam);

        Geoprocessor gpTask = new Geoprocessor(URL, creds);

        if(downloadRouteTask != null){

            if(downloadRouteTask.getStatus() == AsyncTask.Status.RUNNING){

                downloadRouteTask.cancel(true);

            }

        }

        downloadRouteTask = new DownloadRouteTask(gpTask, status);

        downloadRouteTask.execute(parameters);

        //submitJobAndPolling(gpTask, parameters);

    }

And this is the AsyncTask class:

    private class DownloadRouteTask extends AsyncTask<ArrayList<GPParameter>, String, GPJobParameter[]> {

        private Geoprocessor gpTask;

        private TextView textView;

        private String msg = "";

        public  DownloadRouteTask(Geoprocessor gpTask, TextView textView){

            this.gpTask = gpTask;

            this.textView = textView;

        }

        @Override

        protected GPJobParameter[] doInBackground(ArrayList<GPParameter>... parameters) {

            GPJobParameter[] outParams = null;

            try {

                GPJobResource gpJobResource = gpTask.submitJob(parameters[0]);

                System.out.println("Polling thread is: " + Thread.currentThread().getName());

                boolean done = false;

                while(!done) {

                    if(isCancelled()){

                        break;

                    }

                    GPJobResource.JobStatus jobStatus = gpJobResource.getJobStatus();

                    switch (jobStatus) {

                        case CANCELLED:

                            msg = "The job has been cancelled based on the client's request. ";

                            done = true;

                            break;

                        case CANCELLING:

                            System.out.println("CANCELLING value on switch");

                            break;

                        case DELETED:

                            textView.setText("The job has been deleted. ");

                            done = true;

                            break;

                        case DELETING:

                            System.out.println("DELETING value on switch");

                            break;

                        case EXECUTING:

                            System.out.println("EXECUTING value on switch");

                            break;

                        case FAILED:

                            msg = "The job execution has failed because of invalid parameters or other geoprocessing failures. ";

                            done = true;

                            break;

                        case NEW_JOB:

                            System.out.println("NEW_JOB value on switch");

                            break;

                        case SUBMITTED:

                            System.out.println("SUBMITTED value on switch");

                            break;

                        case SUCCEEDED:

                            msg = "The job has completed successfully and the output results are available. ";

                            outParams = gpJobResource.getOutputParameters();

                            done = true;

                            break;

                        case TIMED_OUT:

                            msg = "The job execution has timed out. ";

                            done = true;

                            break;

                        case WAITING:

                            System.out.println("WAITING value on switch");

                            break;

                        default:

                            System.out.println("default value on switch");

                    }

                    try {

                        Thread.sleep(4000);

                    } catch (InterruptedException e) {

                    }

                }

            }

            catch(EsriSecurityException e){

                if(e.getCode() == -10001){

                    msg = "There were no user with that name and password.";

                    return null;

                }

            }

            catch (Exception e) {

                msg = e.getLocalizedMessage();

                e.printStackTrace();

            }

            return outParams;

        }

        @Override

        protected void onPostExecute(GPJobParameter[] result) {

            if(msg.length() > 0){

                textView.setTextColor(getResources().getColor(R.color.text_dark_grey));

                textView.setText(msg);

            }

            if(result != null) {

                if ( result.length > 0) {

                } else {

                    textView.setTextColor(getResources().getColor(R.color.text_dark_grey));

                    textView.setText("There were no route with that id");

                }

            }

        }

        // If the cancellation occurs, set the message informing so

        @Override

        protected void onCancelled() {

            textView.setTextColor(getResources().getColor(R.color.text_orange));

             textView.setText("Job cancelled");

            super.onCancelled();

        }

        @Override

        public void onPreExecute(){

            textView.setTextColor(getResources().getColor(R.color.text_dark_grey));

            textView.setText("Fetching the route" );

        }

    }

I have made an app in Objective-C that does the same and it works so the service works.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ÅsaWegelius
Deactivated User

I gave up and investigated how to make the handler work instead and found a snippet that solved the issue I had.

I put this in the onCreate:

        if (android.os.Build.VERSION.SDK_INT > 9)

        {

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy);

        }

and I start the job:

        Geoprocessor gpTask = new Geoprocessor(URL, creds);

        //Geoprocessor gpTask = new Geoprocessor(URL);

        if(handler != null){

            handler.removeCallbacks(r);

        }

        handler = new Handler();

        submitJobAndPolling(gpTask, parameters);

and this is the method :

   void submitJobAndPolling(final Geoprocessor gpTask,

                             List<GPParameter> params) {

        try {

            GPJobResource gpJobResource = gpTask.submitJob(params);

            GPJobResource.JobStatus jobstatus = gpJobResource.getJobStatus();

            final String jobid = gpJobResource.getJobID();

            status.setTextColor(getResources().getColor(R.color.text_dark_grey));

            GPLong routeNo = (GPLong)params.get(0);

            status.setText("Fetching route " + routeNo.getValue());

            if (jobstatus != GPJobResource.JobStatus.SUCCEEDED) {

                handler.postDelayed(r = new Runnable() {

                    @Override

                    public void run() {

                        try {

                            GPJobResource gpJobResource1 = gpTask.checkJobStatus(jobid);

                            boolean jobcomplete = false;

                            GPJobResource.JobStatus jobStatus = gpJobResource1.getJobStatus();

                            switch (jobStatus) {

                                case CANCELLED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job has been cancelled based on the client's request. ");

                                    jobcomplete = true;

                                    break;

                                case CANCELLING:

                                    break;

                                case DELETED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job has been deleted. ");

                                    jobcomplete = true;

                                    break;

                                case DELETING:

                                    break;

                                case EXECUTING:

                                    break;

                                case FAILED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job execution has failed because of invalid parameters or other geoprocessing failures. ");

                                    jobcomplete = true;

                                    break;

                                case NEW_JOB:

                                    break;

                                case SUBMITTED:

                                    break;

                                case SUCCEEDED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("Added the route! ");

                                    jobcomplete = true;

                                    break;

                                case TIMED_OUT:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job execution has timed out. ");

                                    jobcomplete = true;

                                    break;

                                case WAITING:

                                    break;

                                default:

                                    break;

                            }

                            if (jobcomplete) {

                                if (jobStatus == GPJobResource.JobStatus.SUCCEEDED) {

                                    System.out.println("GP succeded");

                                }else {

                                    System.out.println("GP failed");

                                }

                            } else {

                                handler.postDelayed(this, 5000);

                            }

                        } catch (Exception e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                        }

                    }

                }, 4000);

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

It works fine now.

View solution in original post

1 Reply
ÅsaWegelius
Deactivated User

I gave up and investigated how to make the handler work instead and found a snippet that solved the issue I had.

I put this in the onCreate:

        if (android.os.Build.VERSION.SDK_INT > 9)

        {

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy);

        }

and I start the job:

        Geoprocessor gpTask = new Geoprocessor(URL, creds);

        //Geoprocessor gpTask = new Geoprocessor(URL);

        if(handler != null){

            handler.removeCallbacks(r);

        }

        handler = new Handler();

        submitJobAndPolling(gpTask, parameters);

and this is the method :

   void submitJobAndPolling(final Geoprocessor gpTask,

                             List<GPParameter> params) {

        try {

            GPJobResource gpJobResource = gpTask.submitJob(params);

            GPJobResource.JobStatus jobstatus = gpJobResource.getJobStatus();

            final String jobid = gpJobResource.getJobID();

            status.setTextColor(getResources().getColor(R.color.text_dark_grey));

            GPLong routeNo = (GPLong)params.get(0);

            status.setText("Fetching route " + routeNo.getValue());

            if (jobstatus != GPJobResource.JobStatus.SUCCEEDED) {

                handler.postDelayed(r = new Runnable() {

                    @Override

                    public void run() {

                        try {

                            GPJobResource gpJobResource1 = gpTask.checkJobStatus(jobid);

                            boolean jobcomplete = false;

                            GPJobResource.JobStatus jobStatus = gpJobResource1.getJobStatus();

                            switch (jobStatus) {

                                case CANCELLED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job has been cancelled based on the client's request. ");

                                    jobcomplete = true;

                                    break;

                                case CANCELLING:

                                    break;

                                case DELETED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job has been deleted. ");

                                    jobcomplete = true;

                                    break;

                                case DELETING:

                                    break;

                                case EXECUTING:

                                    break;

                                case FAILED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job execution has failed because of invalid parameters or other geoprocessing failures. ");

                                    jobcomplete = true;

                                    break;

                                case NEW_JOB:

                                    break;

                                case SUBMITTED:

                                    break;

                                case SUCCEEDED:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("Added the route! ");

                                    jobcomplete = true;

                                    break;

                                case TIMED_OUT:

                                    status.setTextColor(getResources().getColor(R.color.text_dark_grey));

                                    status.setText("The job execution has timed out. ");

                                    jobcomplete = true;

                                    break;

                                case WAITING:

                                    break;

                                default:

                                    break;

                            }

                            if (jobcomplete) {

                                if (jobStatus == GPJobResource.JobStatus.SUCCEEDED) {

                                    System.out.println("GP succeded");

                                }else {

                                    System.out.println("GP failed");

                                }

                            } else {

                                handler.postDelayed(this, 5000);

                            }

                        } catch (Exception e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                        }

                    }

                }, 4000);

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

It works fine now.