简体   繁体   English

通过 API 向 openHAB 发送 Android Volley POST 请求

[英]Send Android Volley POST request to openHAB via API

I'm trying to send a volley POST request to my openHAB instance via the given API.我正在尝试通过给定的 API 向我的 openHAB 实例发送一个凌空 POST 请求。 Inside the openHAB dashboard there is the opportunity to test the API calls and there the POST request is working and looks like this:在 openHAB 仪表板内,有机会测试 API 调用,POST 请求正在工作,如下所示:

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".在请求末尾的部分我可以切换我的名为“DummySwitch”的项目的 state。

Now i want to call the exact same request inside my Android application.现在我想在我的 Android 应用程序中调用完全相同的请求。 A general GET request that looks like this:如下所示的一般 GET 请求:

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:现在我尝试使用以下代码执行 POST 请求:

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.我不确定 getHeaders 方法是否必要,因为无论是否使用它都没有任何区别。 I think the problem is that i don't have a key for the given data "ON".我认为问题在于我没有给定数据“ON”的密钥。 The request as shown above only uses -d "ON" to send the new state of the item.如上所示的请求只使用了 -d "ON" 来发送项目的新 state。

Does anyone know how to fix this and getting the POST request working?有谁知道如何解决这个问题并让 POST 请求正常工作?

Thank you very much.非常感谢。

I found the solution.我找到了解决方案。 You have to use the @Override method getBody to send text/plain requests:您必须使用 @Override 方法 getBody 发送文本/纯文本请求:

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

There you can edit the "ON" field like you need.在那里,您可以根据需要编辑“ON”字段。

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: getHeaders 方法中的字段改为使用以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM