我正在尝试使用Volley框架的Post方法将自定义对象发送到服务器端。我知道应该重载getBody方法。但是什么被指定为身体内容类型? getBody()和getBodyContentType()方法的代码将节省我的一天。
这是我的Model类:
package ar.sample.model;
import java.sql.Timestamp;
public class Location {
private Integer id;
private String locationName;
private Double latitude;
private Double longitude;
private Float radius;
private String logicalGroup;
private String parentLocation;
private String country;
private String city;
private String type;
private String description;
private Integer status;
private Timestamp createdTime;
private Timestamp updatedTime;
private String createdByUser;
private String updatedByUser;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Float getRadius() {
return radius;
}
public void setRadius(Float radius) {
this.radius = radius;
}
public String getLogicalGroup() {
return logicalGroup;
}
public void setLogicalGroup(String logicalGroup) {
this.logicalGroup = logicalGroup;
}
public String getParentLocation() {
return parentLocation;
}
public void setParentLocation(String parentLocation) {
this.parentLocation = parentLocation;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Timestamp getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Timestamp createdTime) {
this.createdTime = createdTime;
}
public Timestamp getUpdatedTime() {
return updatedTime;
}
public void setUpdatedTime(Timestamp updatedTime) {
this.updatedTime = updatedTime;
}
public String getCreatedByUser() {
return createdByUser;
}
public void setCreatedByUser(String createdByUser) {
this.createdByUser = createdByUser;
}
public String getUpdatedByUser() {
return updatedByUser;
}
public void setUpdatedByUser(String updatedByUser) {
this.updatedByUser = updatedByUser;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("locationName", locationName)
.add("latitude", latitude)
.add("longitude", longitude)
.add("radius", radius)
.add("logicalGroup", logicalGroup)
.add("parentLocation", parentLocation)
.add("country", country)
.add("city", city)
.add("type", type)
.add("description", description)
.add("status", status)
.add("createdTime", createdTime)
.add("updatedTime", updatedTime)
.add("createdByUser", createdByUser)
.add("updatedByUser", updatedByUser)
.toString();
}
}
我在服务器端的代码是:
@RequestMapping(value = "/location", method = RequestMethod.POST)
public Location createLocationComplete(@RequestBody @Valid Location location) {
LOGGER.debug("API: Create {}", location);
//location = composeLocation(location);
return locationService.insert(location);
}
如何使用Volley框架通过POST方法发送位置对象?
答案 0 :(得分:0)
您可以通过这种方式发出截击请求 - >
String url = "";
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// You will get the response here
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// You will get error here
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<>();
map.put("parameter",STRING_VALUE); // Set the parameter and the value which has been passing to your server here.
return map;
}
};
queue.add(stringRequest);