0

I am trying to send the custom object to the server side using Volley framework's Post method. i know the getBody method should be overloaded. But what to be specified as body content type? code of getBody() and getBodyContentType() methods will save my day.

This is my Model class:

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

}

My code at server side is:

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

How to send the location object through POST method using Volley framework?

halfer
  • 19,824
  • 17
  • 99
  • 186
Abhishek
  • 61
  • 11

1 Answers1

0

You can make a volley request by this way ->

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);
Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
  • Thanks for answering. But on the server side i am using @RequestBody. I have pasted the code in the question. Will passing the parameters help? – Abhishek Jun 02 '16 at 06:40
  • what's the perimeter name you receiver data from the server side ? – Zahidul Islam Jun 02 '16 at 06:42
  • public Location createLocationComplete(@RequestBody @Valid Location location) { LOGGER.debug("API: Create {}", location); //location = composeLocation(location); return locationService.insert(location); } – Abhishek Jun 02 '16 at 06:46
  • I don't see any `@RequestParam` here. You have to add `params = { "locationData" }` in `@RequestMapping` and `@RequestParam("locationData") String locData` as a parameter of `createLocationComplete()` method . – Zahidul Islam Jun 02 '16 at 06:57