AnsweredAssumed Answered

Geoprocessor and AsyncTask

Question asked by asawegelius on Mar 4, 2015
Latest reply on Mar 5, 2015 by asawegelius

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.

Outcomes