Friday, August 7, 2009

Generating JSON with Java App Engine

1. Requirements App Engine 1.2.2. (The older version of App Engine has a problem compiling the org.json package)

2. Download Java JSON.zip from http://www.json.org

3. Read my first app engine tutorial.

4. Generate a JSON string from any java.util.List of Objects. For this instance I used the objects from #3.


public class BeerWS extends HttpServlet {

public void process(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
resp.setContentType("text/plain");
PersistenceManager persistenceManager = PMF.get()
.getPersistenceManager();
String query = "select from " + Beer.class.getName()
+ " order by name asc";
List beerLocations = (List)persistenceManager.newQuery(query).execute();
List list = new ArrayList();
for(Beer beer : beerLocations) {
//Add the pojo as a JSONObject
list.add(new JSONObject(beer));
}
//Create a JSONArray based from the list of JSONObejcts
JSONArray jsonArray = new JSONArray(list);
//Then output the JSON string to the servlet response
resp.getWriter().println(jsonArray.toString());
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
process(req, resp);
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
process(req, resp);
}
}


5. Here's the link of the live demo. http://beer.androidph.com/beerws. Below is the object used if you have not already check my previous app engine tutorial.


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Beer {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String name;
@Persistent
private Double price;
@Persistent
private Date lastCallTime;
@Persistent
private String promotions;
@Persistent
private String address;
@Persistent
private Double longitude;
@Persistent
private Double latitude;

public Beer() {}

public Beer(String name, Double price,
Date lastCallTime, String promotions,
String address, Double longitude, Double latitude) {
this.name = name;
this.price = price;
this.lastCallTime = lastCallTime;
this.promotions = promotions;
this.address = address;
this.longitude = longitude;
this.latitude = latitude;
}

public Beer(Long id, String name, Double price,
Date lastCallTime, String promotions, String address,
Double longitude, Double latitude) {
this.id = id;
this.name = name;
this.price = price;
this.lastCallTime = lastCallTime;
this.promotions = promotions;
this.address = address;
this.longitude = longitude;
this.latitude = latitude;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getLastCallTime() {
return lastCallTime;
}
public void setLastCallTime(Date lastCallTime) {
this.lastCallTime = lastCallTime;
}
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
public String getLastCallTimeString() {
if(lastCallTime != null) {
return sdf.format(lastCallTime);
} else {
return "-";
}
}
public String getPromotions() {
return promotions;
}
public void setPromotions(String promotions) {
this.promotions = promotions;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
}

3 comments:

java.padawan said...

I'll post the Android JSON parser this weekend.

Unknown said...

Great tutorial---easy enough!

Thanks.

java.padawan said...

Android Parsing JSON Tutorial

Email

java.padawan@androidph.com