androidandroid-fragmentsbackendlessmbaas

Android Studio - Deleting Bulk Data From Backendless Data Table


I have a table called "LocalPhoneNum" in the Backendless Server. It contains three objects. Each with a name, phone number and email. All three of these objects have the same email. Here's what it looks like.

enter image description here

I need to figure out how to delete all three of the objects programmatically from my Android application using the "Bulk Delete" option found in the Backendless REST API: https://backendless.com/documentation/data/rest/data_deleting_data_objects.htm

I'd like for the delete to happen as soon as my Fragment's onCreateView() method is instantiated. My application is very simple. A Fragment is launched, an ASyncTask Class is called, and the data is deleted in the background. My issue is that I do not have a lot of experience making these types of calls to a server and simply don't know the correct syntax for this to work. I've been searching everywhere but can't find a clean answer.

Here is what I have so far:

  1. My Fragment class contains a private String called API_URL, which contains the appropriate URL to delete the objects according to the Backendless instructions found here: enter image description here

This is what my Fragment Class Looks Like:

public class ContactsFragments extends Fragment {
private View view;
private final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%3Dmark@gmail.com";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view =  inflater.inflate(R.layout.fragment_contacts_fragments, container, false);

    new DeleteBulkFromBackEnd().execute();

    return view;
}}

Here is the ASyncTask() Class that is called to do the work:

class DeleteBulkFromBackEnd extends AsyncTask<Void,Void,String>{
    StringBuilder stringBuilder;

    @Override
    protected String doInBackground(Void... params) {
        HttpURLConnection urlConnection = null;


        try {
            URL url = new URL(API_URL);
            urlConnection = (HttpURLConnection)url.openConnection();

         urlConnection.setRequestProperty( "application-id","12345678" );
            urlConnection.setRequestProperty( "secret-key","12345678" );
            urlConnection.setRequestProperty( "application-type", "REST" );
            urlConnection.connect();



        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }finally {
            urlConnection.disconnect();
        }

        return null;
    }

}

My main question is, what am I missing here syntactically? When I copy and paste the url that my app is using (https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%3Dmark@gmail.com) into a browser, the page displays: {"code":2002,"message":"Version is disabled or provided wrong application info (application id or secret key)"}

Clearly something is missing but I don't know how to make the connection from point A to point B.

The tutorial also mentions Request Headers. Although I have all of the information that it asks for (application-id, secret-key, app-type) in the tutorial (Link above), I'm not sure how to incorporate them in my code?

If anyone knows what I am doing wrong or could help me out, I would greatly appreciate it. Hopefully that is enough details.


Solution

  • You need to use the "setRequestProperty" method on the HttpURLConnection object:

    urlConnection.setRequestProperty( "application-id", "VALUE" );
    urlConnection.setRequestProperty( "secret-key", "VALUE" );
    

    Also, the email address value in the URL (specifically in the "where" argument) must be surrounded by single quotes, which also must be URL encoded. This is what it must look like:

    https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where=userEmailD%3D%27mark%40gmail.com%27
    

    Entering that value into a browser will do nothing. Since the browser will send it as GET request (instead of DELETE) and none of the required headers are sent.