Mark F
Mark F

Reputation: 1543

Accessing REST API in Android Using Request Headers

I have a question about the proper syntax and order of code in regards to accessing a REST API.

I am trying to access a database that I made on an mBaas called backendless.com. (The following data information is specific to this mBaas but my question is more about the general process of accessing REST API's in Android)

According to their tutorial to bulk delete (https://backendless.com/documentation/data/rest/data_deleting_data_objects.htm) I need a URL that queries my database for a specific value and then deletes it. I have that value. They also need 3 request headers (application-id, secret key, application type).I have those as well.

I utilized all of this information in an ASyncTask class that technically should open the url, set the request headers, and make the call to the REST API. My only issue is, I have no idea if I'm missing some kind of code here? Is my current code in proper order? Every time my class is executed, nothing happens.

I also get a log cat exception in regards to my URL: java.io.FileNotFoundException: api.backendless.com/v1/data/bulk/...

The URL does not lead to anything when I place it in my browser but I'm told that it shouldn't since the browser sends it as a GET request.

Anyway, here is my ASyncTask Class with all of the info. Does anyone know if this code looks correct or am I missing something here? I'm new to making these type of calls and don't really understand the roll that request-headers play in accessing REST APIs. Please let me know. Thank you!

 class DeleteBulkFromBackEnd extends AsyncTask<Void,Void,String>{
   final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%[email protected]";

    @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) {
            Log.d("Contact","ERROR " + e.toString() );//IO Exception Prints in log cat not recognizing URL
            e.printStackTrace();
        }finally {
            urlConnection.disconnect();
        }

        return null;
    }

}

Upvotes: 1

Views: 3806

Answers (2)

F&#225;bio Hiroki
F&#225;bio Hiroki

Reputation: 555

As I've commented, here's the solution:

 class DeleteBulkFromBackEnd extends AsyncTask<Void,Void,String>{
   final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%[email protected]";

    @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.setRequestMethod("DELETE");

            urlConnection.connect();



        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {
            Log.d("Contact","ERROR " + e.toString() );//IO Exception Prints in log cat not recognizing URL
            e.printStackTrace();
        }finally {
            urlConnection.disconnect();
        }

        return null;
    }

}

Upvotes: 1

nshmura
nshmura

Reputation: 6010

I recommend you to use okhttp for easy network access.

And check the response code and response body.

In your build.gradle:

compile 'com.squareup.okhttp3:okhttp:3.4.1'

Your AsyncTask will be like this:

class DeleteBulkFromBackEnd extends AsyncTask<Void, Void, String> {

    final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%[email protected]";
    final OkHttpClient mClient;

    public DeleteBulkFromBackEnd(OkHttpClient client) {
        mClient = client;
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            Request request = new Request.Builder()
                    .url(API_URL)
                    .delete()
                    .header("application-id", "12345678")
                    .header("secret-key", "12345678")
                    .header("application-type", "REST")
                    .build();

            Response response = mClient.newCall(request).execute();

            Log.d("DeleteBulkFromBackEnd", "Code: " + response.code());
            Log.d("DeleteBulkFromBackEnd", "Body: " + response.body().string());

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

        return null;
    }
}

Execute the AsyncTask like this:

OkHttpClient client = new OkHttpClient();

void someMethod() {
    ...
    new DeleteBulkFromBackEnd(client).execute();
    ...
}

Upvotes: 2

Related Questions