【问题标题】:Android post request with non key pair具有非密钥对的 Android 发布请求
【发布时间】:2017-01-01 01:39:19
【问题描述】:

以下是我目前使用家庭自动化系统打开灯的工作代码。我正在升级到一个新系统,它需要修改后的命令。目前我认为这会发送密钥对“lightswitch1=ON”。我需要它只发送“ON”,但我不知道该怎么做。下面是有效的 CURL 命令。

public void turnonlight() {


    String url = "http://example.com:8090/CMD";


    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //Toast.makeText(MjpegActivity.this,response,Toast.LENGTH_LONG).show();
                    //This code is executed if the server responds, whether or not the response contains data.
                    //The String 'response' contains the server's response.
                }
        },
            new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Toast.makeText(MjpegActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    //This code is executed if there is an error.
                }
        }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put(lightswitch1 "ON"); //Add the data you'd like to send to the server.
            return MyData;
        }
    };
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    MyRequestQueue.add(MyStringRequest);
}

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://example.com:8090/CMD"

【问题讨论】:

    标签: java android http curl post


    【解决方案1】:

    以下内容应与 Volley 一起使用:

    String url = "http://example.com:8090/CMD";
    
    StringRequest myStringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            return headers;
        }
    
        @Override
        public String getBodyContentType() {
            return "text/plain";
        }
    
        @Override
        public byte[] getBody() throws AuthFailureError {
            String httpPostBody = "ON";
            return httpPostBody.getBytes();
        }
    };
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    myStringRequest.setShouldCache(false);
    MyRequestQueue.add(myStringRequest);
    

    正文被getBody()覆盖,指定的标题被getHeaders()覆盖。

    【讨论】:

    • 我在尝试将其与您的代码一起发送时收到“BasicNetwork.performRequest:意外的响应代码 400”错误。我使用了钢丝鲨并嗅探了一个工作请求和一个非工作请求。右边有效,左边无效。我可以改变什么来让左边的那个和右边的一样?
    • 我认为是因为内容类型错误。
    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多