用Volley做一个POST请求

时间:2016-09-16 05:05:03

标签: java android android-volley

嘿我想在我的Android应用程序中做这样的请求:

curl --header "Content-Type: text/plain" --request POST --data "ON" http://example.com:8080/rest/items/wakeup

到目前为止,我有这个:

String url = "http://example.com:8080/rest/items/wakeup";

    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("AAAAAA", "BBBBBB"); //Add the data you'd like to send to the server.
            return MyData;
        }
    };
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    MyRequestQueue.add(MyStringRequest);
}

这取自这里:https://www.simplifiedcoding.net/android-volley-post-request-tutorial/ 有人可以帮我弄清楚如何使这项工作?我知道我发送的数据是AAAAAA和BBBBBB所在,但我真的不明白我怎样才能发送字符串&#34; ON&#34;。

1 个答案:

答案 0 :(得分:0)

打开与服务器端口80的连接并发送此文本(将i.php替换为您要发布到的文件)

POST /i.php HTTP/1.1
Host: denta.dev:8081
User-Agent: curl/7.47.0
Accept: */*
Content-Type: text/plain
Content-Length: 2

ON
相关问题