我知道也有类似的问题,但我已经搜索了许多问题,但这些问题似乎对我没有帮助。所以我想做的就是发送一个简单的字符串"ON"/ "OFF"
作为数据的发布请求。像这样的东西(对于某些$ URL)起作用:
curl -X POST --header "Content-Type: text/plain; charset=utf-8" -d "ON" "$URL"
这是我正在尝试的代码,在阅读了很多代码和答案后,我认为它可以正常工作,但事实并非如此。
我想念什么?我从服务器得到一个
400 error
。Request request = new StringRequest
(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Command was sent successfully");
}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Got command error for " + url + " command " + command + " error " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/plain; charset=utf-8");
// headers.put("Accept", "application/json");
// headers.put("User-agent", "My useragent");
return headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return command.getBytes("utf-8");
} catch (Exception e) {
Log.d(TAG, "failed converting" + e.getMessage());
return null;
}
}
};
最佳答案
您必须重写getParams()
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("postData", "my simple on/off string");
return params;
};
关于android - Volley android库发布请求不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35408272/