In addition to Mark's code snippet, here's some additional code taken from a Clip and Ship demo that shows another pattern for coding against an asynchronous GP service. The full sample code will be included with the v3 release of our SDK later this year. Or, if you want to see the full app sooner then ping me: agup at esri dot com.strmv.setParamName("Layers_to_Clip");
strmv.setValues(arrayList);
final ArrayList<GPParameter> paramlist = new ArrayList<GPParameter>();
paramlist.add(strmv);
paramlist.add(areaofinterest);
paramlist.add(featureformat);
handler = new Handler();
submitJobandPolling(gp, paramlist);
/**
* Method submitJobandPolling.
* @param gp Geoprocessor
* @param params List<GPParameter>
*/
void submitJobandPolling(final Geoprocessor gp,
List<GPParameter> params) {
try {
GPJobResource gpjr1 = gp.submitJob(params);
JobStatus jobstatus = gpjr1.getJobStatus();
final String jobid = gpjr1.getJobID();
Log.d("Test", "jobid " + jobid);
Log.d("Test", "jobstatus " + jobstatus);
// if (!jobstatus.equals("esriJobSucceeded")) {
if (jobstatus != JobStatus.succeeded) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
GPJobResource gpjr2 = gp.checkJobStatus(jobid);
GPMessage[] messages = gpjr2.getMessages();
if ( messages!= null && messages.length > 0) {
for (int i = 0; i < messages.length; i++) {
Log.d("Test", "Message: " + messages.getDescription());
}
}
Log.d("Test", "Polling thread is: "
+ Thread.currentThread().getName());
JobStatus status = gpjr2.getJobStatus();
boolean jobcomplete = false;
if (status == JobStatus.canceled
|| status == JobStatus.deleted
|| status == JobStatus.failed || status == JobStatus.succeeded
|| status == JobStatus.timedOut) {
jobcomplete = true;
}
if (jobcomplete) {
if (status == JobStatus.succeeded) {
GPDataFile outputZipfile = (GPDataFile) gp
.getResultData(jobid, "Output_Zip_File");
Log.d("Test", "zip file is "
+ outputZipfile.getUrl().toURI()
.toString());
}else {
Log.d("Test", "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();
}
}