Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Monday, August 10, 2009

Android Parsing JSON

1. Parse the live JSON response from http://beer.androidph.com/beerws. See App Engine Generating JSON.

2. Use the code from Android Networking Tutorial

3. JSON Parser Code



JSONArray parseArray = new JSONArray(message);
for (int i = 0; i < jo =" parseArray.getJSONObject(i);


4. Modify nextscreen.xml of the source code from #2.


















5. Modify NextScreen.java from source code in #2 or Android Networking Tutorial


public class NextScreen extends Activity implements OnClickListener {
private Button btnBack;

private ViewGroup.LayoutParams layoutName;
private ViewGroup.LayoutParams layoutAddress;
private ViewGroup.LayoutParams layoutPrice;
private ViewGroup.LayoutParams layoutLastCallTime;
private ViewGroup.LayoutParams layoutLocation;

private String message;
private LinearLayout linearLayout;
private LinearLayout linearInnerLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextscreen);
initComponents();
}

public void initComponents() {

linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
linearInnerLayout = (LinearLayout) linearLayout.getChildAt(0);

layoutName = ((TextView) linearInnerLayout.getChildAt(0))
.getLayoutParams();
layoutAddress = ((TextView) linearInnerLayout.getChildAt(1))
.getLayoutParams();
layoutPrice = ((TextView) linearInnerLayout.getChildAt(2))
.getLayoutParams();
layoutLastCallTime = ((TextView) linearInnerLayout.getChildAt(3))
.getLayoutParams();
layoutLocation = ((TextView) linearInnerLayout.getChildAt(4))
.getLayoutParams();

message = getIntent().getStringExtra(NetworkConnection.NC_RESPONSE);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

try {
JSONArray parseArray = new JSONArray(message);
for (int i = 0; i < parseArray.length(); i++) {
JSONObject jo = parseArray.getJSONObject(i);

LinearLayout newRow = new LinearLayout(this);
newRow.setLayoutParams(linearInnerLayout.getLayoutParams());

String name = jo.getString("name") + " | ";
TextView curTvName = new TextView(this);
curTvName.setText(name);
curTvName.setLayoutParams(layoutName);

String address = jo.getString("address") + " | ";
TextView curTvAddress = new TextView(this);
curTvAddress.setText(address);
curTvAddress.setLayoutParams(layoutAddress);

String price = jo.getString("price") + " | ";
TextView curTvPrice = new TextView(this);
curTvPrice.setText(price);
curTvPrice.setLayoutParams(layoutPrice);

String lastCallTime = jo.getString("lastCallTimeString") + " | ";
TextView curTvLastCallTime = new TextView(this);
curTvLastCallTime.setText(lastCallTime);
curTvLastCallTime.setLayoutParams(layoutLastCallTime);

String location = jo.getString("latitude") + "," + jo.getString("longitude");
TextView curTvLocation = new TextView(this);
curTvLocation.setText(location);
curTvLocation.setLayoutParams(layoutLocation);

newRow.addView(curTvName);
newRow.addView(curTvAddress);
newRow.addView(curTvPrice);
newRow.addView(curTvLastCallTime);
newRow.addView(curTvLocation);

linearLayout.addView(newRow, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void onClick(View v) {
if (v == btnBack) {
finish();
}
}
}



6. Screenshot of the android JSON parser.

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

Email

java.padawan@androidph.com