Reputation: 49
My listBo class
public class ListBO {
public String itemName = "";
public String itemType = "";
public String itemImg = "";
public String itemPrice = "";
public int itemId;
/**
* @return the itemName
*/
public String getItemName() {
return itemName;
}
/**
* @param itemName the itemName to set
*/
public void setItemName(String itemName) {
this.itemName = itemName;
}
/**
* @return the itemType
*/
public String getItemType() {
return itemType;
}
/**
* @param itemType the itemType to set
*/
public void setItemType(String itemType) {
this.itemType = itemType;
}
/**
* @return the itemImg
*/
public String getItemImg() {
return itemImg;
}
/**
* @param itemImg the itemImg to set
*/
public void setItemImg(String itemImg) {
this.itemImg = itemImg;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemPrice(){
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
My listjson class
@Path("/listItems")
public class ListJson {
@Path("/lists")
@POST
@Produces(MediaType.APPLICATION_JSON)
public List<ListBO> getList(){
System.out.println("Called Main JSON");
List<ListBO> lists = new ArrayList<ListBO>();
for (int i = 0; i < 10; i++) {
ListBO items = new ListBO();
items.setItemId(i);
items.setItemName("Chicken :"+i);
items.setItemType("Type :"+i);
items.setItemPrice("Price :"+i);
if(i<=10){
items.setItemPrice("20");
}
lists.add(items);
}
return lists;
}
i can add image like this but it doesnot return an image please helpme..
/*@Path("/image") class ImageService {
private static final String FILE_PATH = "c:\images.jpg";
@GET
@Path("/get")
@Produces("image/jpg")
public Response getFile() {
File file = new File(FILE_PATH);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=image_from_server.jpg");
return response.build();
}
} */
}
how to return an image and where can i store an image give me an example..
Upvotes: 1
Views: 2748
Reputation: 22637
you can't return an image in a JSON object. JSON is not meant for binary data. your web service should return JSON containing a field that is a reference to the image, a URL. then the code that consumes the web service must get the URL from the JSON and fetch the image from the URL.
{ imageUrl: "http://foo.com/image.png" }
it's hard to say exactly what you are doing without more information. you also might want to re-tag this as jersey or resteasy, not android. i think the code you posted is a jersey resource, looking at the annotations.
Upvotes: 1