【问题标题】:Sending Custom object through post method using Volley in Android在Android中使用Volley通过post方法发送自定义对象
【发布时间】:2016-06-02 05:59:57
【问题描述】:

我正在尝试使用 Volley 框架的 Post 方法将自定义对象发送到服务器端。我知道 getBody 方法应该被重载。但是要指定为正文内容类型的内容是什么? getBody() 和 getBodyContentType() 方法的代码将节省我的时间。

这是我的模型类:

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 方法发送位置对象?

【问题讨论】:

  • 您想使用 volley 从 android 向服务器发送值吗?
  • @YounasBangash:是的。
  • 你想在 php 中接收那些参数?
  • @YounasBangash:Nope.its Spring boot。

标签: android android-volley


【解决方案1】:

你可以通过这种方式提出截击请求->

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);

【讨论】:

  • 感谢您的回答。但在服务器端我使用@RequestBody。我已将代码粘贴到问题中。传递参数会有帮助吗?
  • 从服务器端接收数据的边界名称是什么?
  • 公共位置 createLocationComplete(@RequestBody @Valid Location location) { LOGGER.debug("API: Create {}", location); //location = composeLocation(location);返回 locationService.insert(location); }
  • 我在这里看不到任何@RequestParam。您必须在@RequestMapping@RequestParam("locationData") String locData 中添加params = { "locationData" } 作为createLocationComplete() 方法的参数。
猜你喜欢
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2014-08-08
  • 1970-01-01
相关资源
最近更新 更多