0

我正在尝试通过给定的 API 向我的 openHAB 实例发送一个凌空 POST 请求。在 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"

随着

-大学教师”

请求结束时的部分我可以切换名为“DummySwitch”的项目的状态。

现在我想在我的 Android 应用程序中调用完全相同的请求。如下所示的一般 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"

使用以下代码可以正常工作:

 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);
}

现在我尝试使用以下代码执行 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);
}

但它不起作用。我不确定 getHeaders 方法是否必要,因为无论是否使用它都没有任何区别。我认为问题在于我没有给定数据“ON”的密钥。如上所示的请求仅使用 -d "ON" 来发送项目的新状态。

有谁知道如何解决这个问题并让 POST 请求正常工作?

非常感谢。

4

1 回答 1

0

我找到了解决方案。您必须使用 @Override 方法 getBody 发送文本/纯文本请求:

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

在那里,您可以根据需要编辑“ON”字段。

还要确保你不使用:

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

getHeaders 方法中的字段改为使用以下方法:

@Override
        public String getBodyContentType() {
            return "text/plain";
        }
于 2021-05-21T12:10:44.380 回答