从Android Volley接收java restful Web服务发送POST请求中的JSON对象
我想要一个小示例,从android volley发送POST请求中的JSON对象,它必须接收Java Restful Web服务,您可以使用以下工作示例代码。希望这有帮助从Android Volley接收java restful Web服务发送POST请求中的JSON对象,java,json,rest,post,android-volley,Java,Json,Rest,Post,Android Volley,我想要一个小示例,从android volley发送POST请求中的JSON对象,它必须接收Java Restful Web服务,您可以使用以下工作示例代码。希望这有帮助 ... try { RequestQueue queue = Volley.newRequestQueue(this); jsonBody = new JSONObject(); jsonBody.put("Title
...
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();
}
。。。
试一试{
RequestQueue=Volley.newRequestQueue(this);
jsonBody=新的JSONObject();
放置(“标题”,“截击应用程序Android演示”);
jsonBody.put(“作者”、“BNK”);
jsonBody.put(“日期”,“2015/08/26”);
requestBody=jsonBody.toString();
StringRequest StringRequest=新的StringRequest(1,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
setText(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
textView.setText(error.toString());
}
}) {
@凌驾
公共字符串getBodyContentType(){
返回String.format(“application/json;charset=utf-8”);
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
return requestBody==null?null:requestBody.getBytes(“utf-8”);
}捕获(不支持的编码异常uee){
wtf(“尝试使用%s获取%s的字节时不支持编码”,
请求主体,“utf-8”);
返回null;
}
}
};
addToRequestQueue(stringRequest);
}捕获(JSONException e){
e、 printStackTrace();
}
此外,您能否澄清
它必须接收Java Restful Web服务的含义?我的上述请求收到字符串值。您可以使用以下工作示例代码。希望这有帮助
...
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();
}
。。。
试一试{
RequestQueue=Volley.newRequestQueue(this);
jsonBody=新的JSONObject();
放置(“标题”,“截击应用程序Android演示”);
jsonBody.put(“作者”、“BNK”);
jsonBody.put(“日期”,“2015/08/26”);
requestBody=jsonBody.toString();
StringRequest StringRequest=新的StringRequest(1,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
setText(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
textView.setText(error.toString());
}
}) {
@凌驾
公共字符串getBodyContentType(){
返回String.format(“application/json;charset=utf-8”);
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
return requestBody==null?null:requestBody.getBytes(“utf-8”);
}捕获(不支持的编码异常uee){
wtf(“尝试使用%s获取%s的字节时不支持编码”,
请求主体,“utf-8”);
返回null;
}
}
};
addToRequestQueue(stringRequest);
}捕获(JSONException e){
e、 printStackTrace();
}
此外,您能否澄清它必须接收Java Restful Web服务的含义?我上面的请求收到字符串值。我们正在通过android volley中的POST请求发送json对象。我想在Java中的restful Web服务中获得相同的json对象,请尝试我的代码,这是以JSONObject为对象的POST请求body@venkatasivaiah:我的回答对你的问题有效吗?我们正在通过android volley中的POST请求发送json对象。我想在Java中的restful Web服务中获得相同的json对象。所以,试试我的代码,这是以JSONObject为对象的POST请求body@venkatasivaiah当前位置我的回答对你的问题有用吗?