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.

No comments:

Email

java.padawan@androidph.com