Store new feature without a map

758
2
02-06-2012 12:24 AM
BojanBukovic
New Contributor
Hello all,

I have a question regarding storing new feature (point in my case) in android application. There is a nice example of adding new feature to the map. It is simple to store this feature to the layer hosted by ArcGIS Server using applyEdits method. In my case I do not need the map, because I would like to store a new feature at my current GPS location, so basically all I need is a button which executes reading of my current GPS position and adding/storing a new point feature at my location.
Is it possible to achieve this as long as I do not want to show/use the map?

Thank you for your help in advance.

Kind Regards
0 Kudos
2 Replies
IvanBespalov
Occasional Contributor III
bojan,

reading of my current GPS position


"Hello Android" book has one small sample of Location Tests:

LocationTest/res/layout/main.xml
LocationTest/src/org/example/locationtest/LocationTest.java
LocationTest/AndroidManifest.xml

all sample sources of this book are here

adding/storing a new point feature at my location


in activity java code (actually to send something to server you need only red part of code):
// on your save button click handler function call AsyncTask
new AddFeaturesTask().execute();

// this may be placed in your activity java class body
private class AddFeaturesTask extends AsyncTask<Void, Integer, Boolean> {
    ProgressDialog progress = null;
    /* (non-Javadoc)
     * @see android.os.AsyncTask#onCancelled()
     */
    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
 super.onPreExecute();
 // do something - show dialog for example
 progress = ProgressDialog.show(...);
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(Boolean result) {
 super.onPostExecute(result);
 // hide progress dialog if it's shown
 if (progress != null && progress.isShowing()) {
            progress.dismiss(); 
 }
 if (result) {
            // do something - alert user... 
 } else {
     // do something - alert user, save data locally ...[depends on application business logic]
 }
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost("your layer url" + "/applyEdits");

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
 // add token, if your services are secured
 nameValuePairs.add(new BasicNameValuePair("token parameter here", "your token"));
 // json array of graphics you need to add - use your own JSON converter or 
 String arrayAsJsonString = MyJsonHelper.fromGraphicsToJsonString(arrGraphics);
 nameValuePairs.add(new BasicNameValuePair("adds", arrayAsJsonString));
        // add response format if you need to parse results
 nameValuePairs.add(new BasicNameValuePair("f", "pjson"));
 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

 // Execute HTTP Post Request
 HttpResponse response = httpClient.execute(httpPost);

 if (response != null) {
     HttpEntity entity = response.getEntity();
     String responseResult = inputStreamToString(entity.getContent());
     // check response result is success to continue
     boolean isSaved = isSuccess(responseResult, 1);
   
     if (isSaved) {
  return true;
     }
 }
    } catch (SSLPeerUnverifiedException spuex) {
 Log.d(TAG, spuex.getMessage());
    } catch (ClientProtocolException cpex) {
 Log.d(TAG, cpex.getMessage());
    } catch (IOException ioex) {
 Log.d(TAG, ioex.getMessage());
    } catch (IllegalArgumentException iaex) {
 Log.d(TAG, iaex.getMessage());
    } catch (Exception ex) {
 Log.d(TAG, ex.getMessage());
    }
    return false;
}

    private boolean isSuccess(String responseResult, int index) {
        if (responseResult != null && responseResult.length() > 0) {
        try {
            JSONObject responseJSON = new JSONObject(responseResult);
         if (responseJSON != null) {
             JSONArray addResultsArray =  responseJSON.getJSONArray(ADDRESULTS_TAG);
         // do something with results if needed
                // parse results and return true if sucess
                return true;
     }
 } catch (JSONException jsex) {
     Log.d(TAG, jsex.getMessage());
 } catch (Exception ex) {
     Log.d(TAG, ex.getMessage());
 }
        return false;
    }

    /**
     * Read response content
     */
    private String inputStreamToString(InputStream is) {
        StringBuilder sb = new StringBuilder();
        String line = "";
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) { 
         sb.append(line);
     }

     rd.close();
        } catch (IOException ioex) {
     Log.d(TAG, ioex.getMessage());
        } catch (Exception ex) {
     Log.d(TAG, ex.getMessage());
        }

        // Return full string
        return sb.toString();
    }
}


from MyJsonHelper class:
private static final String X_KEY = "x";
private static final String Y_KEY = "y";
private static final String GEOMETRY_KEY = "geometry";
private static final String ATTRIBUTES_KEY = "attributes";



/**
 * @param array of com.esri.core.map.Graphic objects
 * @return String representation of JSON array
 */
public static String fromGraphicsToJsonString(Graphic[] graphics) {
    if (graphics != null) {
        try {
     JSONArray arr = new JSONArray();
     for (Graphic graphic : graphics) {
         Geometry geometry = graphic.getGeometry();
  Map<String, Object> attributes = graphic.getAttributes();
     
  if (geometry != null && geometry.getType() == Geometry.Type.Point && attributes != null) {
      Point pt = (Point)geometry;
      double xValue = pt.getX();
      double yValue = pt.getY();
      Map<String, Double> geometryMap = new TreeMap<String, Double>();
      geometryMap.put(X_KEY, xValue);
      geometryMap.put(Y_KEY, yValue);
      JSONObject geometryObj = new JSONObject(geometryMap);
      JSONObject attributesObj = new JSONObject(attributes);
     
      Map<String, JSONObject> feature = new TreeMap<String, JSONObject>();
      feature.put(GEOMETRY_KEY, geometryObj);
      feature.put(ATTRIBUTES_KEY, attributesObj);
      
      JSONObject featureObj = new JSONObject(feature);
      arr.put(featureObj);
  } else {
      // add empty feature
      JSONObject geometryObj = new JSONObject();
      JSONObject attributesObj = new JSONObject();
      
      Map<String, JSONObject> feature = new TreeMap<String, JSONObject>();
      feature.put(GEOMETRY_KEY, geometryObj);
      feature.put(ATTRIBUTES_KEY, attributesObj);
      
      JSONObject featureObj = new JSONObject(feature);
      arr.put(featureObj);
  }
            }
    
     if (arr != null && arr.length() > 0) {
                return arr.toString();
            }
 } catch (NullPointerException npex) {
     if (npex.getMessage() != null)
         Log.d(TAG, npex.getMessage());
     else
         Log.d(TAG, npex.toString());
 } catch (Exception ex) {
            Log.d(TAG, ex.getMessage());
 }
    }
    return null;
}
0 Kudos
BojanBukovic
New Contributor
Ivan,

thank you very much for your quick reply. The "JSON and sending data" part will be very helpful.

Kind Regards,
Bojan
0 Kudos