【发布时间】:2015-12-05 00:48:19
【问题描述】:
我想要一个小例子来发送来自 android volley 的 POST 请求中的 JSON 对象,它必须接收 Java Restful Webservices
【问题讨论】:
标签: java json rest post android-volley
我想要一个小例子来发送来自 android volley 的 POST 请求中的 JSON 对象,它必须接收 Java Restful Webservices
【问题讨论】:
标签: java json rest post android-volley
您可以使用以下工作示例代码。希望这会有所帮助!
...
try {
RequestQueue queue = Volley.newRequestQueue(this);
jsonBody = new JSONObject();
jsonBody.put("Title", "VolleyApp Android Demo");
jsonBody.put("Author", "BNK");
jsonBody.put("Date", "2015/08/26");
requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(1, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
textView.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText(error.toString());
}
}) {
@Override
public String getBodyContentType() {
return String.format("application/json; charset=utf-8");
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
requestBody, "utf-8");
return null;
}
}
};
queue.addToRequestQueue(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
另外,你能澄清一下it has to receive Java Restful Webservices 是什么意思吗?我的上述请求收到 String 值。
【讨论】: