【发布时间】: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”。
【问题讨论】:
-
字符串 "ON" 是什么意思?你能澄清一下吗?你想知道如何给这个发送参数吗?
-
见the answer here,第二个例子。您可能只希望
return String("ON").getBytes();获得getBody()而不是getParams()。 -
很高兴看到有人从我的博客提出问题,任何帮助都来自我:)
-
为什么不直接使用Java自带的HttpUrlConnection呢?这是我在网上找到的最好的教程:mkyong.com/java/how-to-send-http-request-getpost-in-java
标签: java android android-volley