Public API – Android DELETE Fence

The following code demonstrates how to delete a fence using the Public API in an Android app using asynchronous URL call; HttpURLConnection is utilized for this particular example.

private final static String PUBLICAPI_URL_FENCE = "https://api.bluedotinnovation.com/1/fences";
 
/**
 * Starts task for deleting a Fence
 * @param customerApiKey
 * @param apiKey
 * @param zoneId
 * @param fenceId
 */
private void executeDeleteFence(String customerApiKey, String zoneId,String fenceId) {
    String postDataUrl = generateDELETEFenceRequest(customerApiKey, zoneId, fenceId);
    new ExecuteDeleteRequest().execute(new String[]{postDataUrl} );
}
  
// AsyncTask to perform the DELETE communication
private class ExecuteDeleteRequest extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        return executeDelete(params[0]);
    }
 
    @Override
    protected void onPostExecute(String result) {
        // Handle result if needed
        handleResult(result);
    }
}
 
/**
 * Executes a DELETE request for given url
 * @param targetURL
 * @return
 */
public String executeDelete(String targetURL)
{
    URL url;
    HttpURLConnection connection = null;
    Log.d(DEBUG_TAG, "executePost(): targetURL=" + targetURL);
 
    try {
        //Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("DELETE");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
 
 
        int http_status = connection.getResponseCode();
        Log.d(DEBUG_TAG, "executeDelete(): http_status=" + http_status);
 
        //Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();
 
    } catch (Exception e) {
        String errorMessage =  e.toString();
        Log.d(DEBUG_TAG, "executeDelete(): " + errorMessage);
        return errorMessage;
 
    } finally {
 
        if(connection != null) {
            connection.disconnect();
        }
    }
}

The following example shows how to form the URL for Deleting a Fence

/**
 * The following example shows how to form the URL for Deleting a Fence
 * @param customerApiKey
 * @param apiKey
 * @param zoneId
 * @param fenceId
 * @return Url String
 */
private String generateDELETEFenceRequest(String customerApiKey,String apiKey,String zoneId, String fenceId){
    String url = null;
    url = PUBLICAPI_URL_FENCE + "?customerApiKey=" +customerApiKey  +"&apiKey="+ apiKey +"&zoneId="+zoneId+"&fenceId=" + fenceId;
    return url;
}
Created by Bluedot DevOps on March 5, 2018

Start the discussion