【问题标题】:Doing a POST request with Volley使用 Volley 执行 POST 请求
【发布时间】:2016-09-16 13:58:04
【问题描述】:

嘿,我希望在我的 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 所在的位置,但我真的不明白如何发送字符串“ON”。

【问题讨论】:

标签: java android android-volley


【解决方案1】:

打开到服务器端口 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

【讨论】:

猜你喜欢
  • 2021-03-11
  • 2019-01-26
  • 2021-10-15
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多