Send Android Volley POST request to openHAB via API

93 views Asked by At

I'm trying to send a volley POST request to my openHAB instance via the given API. Inside the openHAB dashboard there is the opportunity to test the API calls and there the POST request is working and looks like this:

curl -X POST "http://172.25.50.100:8080/rest/items/DummySwitch" -H  "accept: */*" -H  "Content-Type: text/plain" -H  "Authorization: Bearer eyJraWQiOm51bGwsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJvcGVuaGFiIiwiYXVkIjoib3BlbmhhYiIsImV4cCI6MTYyMDM5MDgwMiwianRpIjoiUFU3R3YwS0RTVWxMb1Nqd1pCZ0o5QSIsImlhdCI6MTYyMDM4NzIwMiwibmJmIjoxNjIwMzg3MDgyLCJzdWIiOiJvcGVuaGFiIiwiY2xpZW50X2lkIjoiaHR0cDovLzE3Mi4yNS41MC4xMDA6ODA4MCIsInNjb3BlIjoiYWRtaW4iLCJyb2xlIjpbImFkbWluaXN0cmF0b3IiXX0.RsagYIzFZS-E8eaKjo7Q6s94Jkt5wgvdVfSseJikJ0vbvCjK2oUGkB12h3jmXNJzzrB8dUpKTu4GhzE3VIUlOMxWifEVEic1o_ZNGWppjyUEAJ80zPE0v1ZUfjyAIH7nopgbkx7WotzVe1qZfhNa5Hi_9zl-beQWcxOUwdBI3vMpF8ZQFAXcNIGeAR2o4NG1RaABGUBYWmnqYDLDCqqPnE6ACMRztIXu0Xv0uDxFkWb0LT8qKdtIkfd2JFDWocTofbHH8kOhvNoBRRowPoISUbLEdRYjppE3_szDpOqEZlafPDCRZ4Ekcm8kmgjx3oYJh3JkpWcZjTw8h1qQzPjVQA" -d "ON"

With the

-d "ON"

part at the end of the request i am able to switch the state of my item called "DummySwitch".

Now i want to call the exact same request inside my Android application. A general GET request that looks like this:

curl -X GET "http://172.25.50.100:8080/rest/items" -H  "accept: application/json" -H  "Authorization: Bearer eyJraWQiOm51bGwsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJvcGVuaGFiIiwiYXVkIjoib3BlbmhhYiIsImV4cCI6MTYyMDM5MDgwMiwianRpIjoiUFU3R3YwS0RTVWxMb1Nqd1pCZ0o5QSIsImlhdCI6MTYyMDM4NzIwMiwibmJmIjoxNjIwMzg3MDgyLCJzdWIiOiJvcGVuaGFiIiwiY2xpZW50X2lkIjoiaHR0cDovLzE3Mi4yNS41MC4xMDA6ODA4MCIsInNjb3BlIjoiYWRtaW4iLCJyb2xlIjpbImFkbWluaXN0cmF0b3IiXX0.RsagYIzFZS-E8eaKjo7Q6s94Jkt5wgvdVfSseJikJ0vbvCjK2oUGkB12h3jmXNJzzrB8dUpKTu4GhzE3VIUlOMxWifEVEic1o_ZNGWppjyUEAJ80zPE0v1ZUfjyAIH7nopgbkx7WotzVe1qZfhNa5Hi_9zl-beQWcxOUwdBI3vMpF8ZQFAXcNIGeAR2o4NG1RaABGUBYWmnqYDLDCqqPnE6ACMRztIXu0Xv0uDxFkWb0LT8qKdtIkfd2JFDWocTofbHH8kOhvNoBRRowPoISUbLEdRYjppE3_szDpOqEZlafPDCRZ4Ekcm8kmgjx3oYJh3JkpWcZjTw8h1qQzPjVQA"

Is working fine with the following code:

 private void executeGetRequest() {

    RequestQueue queue = Volley.newRequestQueue(this);

    final String url = "http://172.25.50.100:8080/rest/items";

    // prepare the Request
    JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>()
            {
                @Override
                public void onResponse(JSONArray response) {
                    // display response
                    Log.i("TEST", "GET Response: " + response.toString());
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("TEST", "Error: " + error.toString());
                }
            }
    );

    // add it to the RequestQueue
    queue.add(getRequest);
}

Now i tried to execute the POST request with the following code:

private void executePostRequest() {

    RequestQueue queue = Volley.newRequestQueue(this);

    final String url = "http://172.25.50.100:8080/rest/items/DummySwitch";

    // prepare the Request
    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    // response
                    Log.i("TEST", "POST Response: " + response);
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("TEST", "POST Error: " + error.toString());
                    error.printStackTrace();
                }
            }
    ) {
        @Override
        public Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("", "ON");

            return params;
        }

        @Override
        public Map<String, String> getHeaders()
        {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Accept","*/*");
            headers.put("Content-Type", "text/plain");
            headers.put("Authorization", "Bearer eyJraWQiOm51bGwsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJvcGVuaGFiIiwiYXVkIjoib3BlbmhhYiIsImV4cCI6MTYyMDM4OTAzMSwianRpIjoidDlYWGpXQVRmb3NabUZoTTlYdDNzdyIsImlhdCI6MTYyMDM4NTQzMSwibmJmIjoxNjIwMzg1MzExLCJzdWIiOiJvcGVuaGFiIiwiY2xpZW50X2lkIjoiaHR0cDovLzE3Mi4yNS41MC4xMDA6ODA4MCIsInNjb3BlIjoiYWRtaW4iLCJyb2xlIjpbImFkbWluaXN0cmF0b3IiXX0.ZuBeXKEzQ2_GUm02xOz9L3IsLBShOWqrMjWX0LcMN639qXGVwY9wYUVKFiXSpVPLXcEO6l-ht6pEAw117gOt36k4YsIw9jIYm8Bl_I_peOiRHg6PcQ3wmgjqbQrBkRxlhHaM7OAMFU3s532WJwsHE4A8VXW32N9S1TxclYq8RPttpglmX_IvF3-DxxLjO9O-X1JH_qOWocm7JDNN6i7XEAxZF60PQyqfZBQT8GoVgHFId26Ciwfo32rC39IO3SRhkTtNsDAKcj9pnwc18luu5FT8uG4uJ6dXewO8w43djkkMMMSWDcR_ox5ixCRs7YYUHQdMs_aRxyNhEE6YWbI3bg");

            return headers;
        }
    };
    queue.add(postRequest);
}

But it doesn't work. I am unsure if the getHeaders-method is necessary or not because it doesn't make any difference by using it or not. I think the problem is that i don't have a key for the given data "ON". The request as shown above only uses -d "ON" to send the new state of the item.

Does anyone know how to fix this and getting the POST request working?

Thank you very much.

1

There are 1 answers

0
DanLand On BEST ANSWER

I found the solution. You have to use the @Override method getBody to send text/plain requests:

@Override
        public byte[] getBody(){
            return "ON".getBytes();
        }

There you can edit the "ON" field like you need.

Also be sure that you don't use the:

headers.put("Content-Type", "text/plain");

field inside the getHeaders-method instead use the following method:

@Override
        public String getBodyContentType() {
            return "text/plain";
        }