0% found this document useful (0 votes)
4 views1 page

Lab 7

This lab focuses on processing JSON messages returned from a server in an Android application. It provides example code to parse a JSON array and extract specific properties such as name and description. Students are encouraged to modify the code to display license plates of cars returned from their previous PHP script.

Uploaded by

Sayuri Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Lab 7

This lab focuses on processing JSON messages returned from a server in an Android application. It provides example code to parse a JSON array and extract specific properties such as name and description. Students are encouraged to modify the code to display license plates of cars returned from their previous PHP script.

Uploaded by

Sayuri Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

Mobile Computing

Lab 7

1 JSON
• This weeks lab will the code from the last lab.

• Some modifications need to be made in order to process the messages that are returned from
the server.

• We need to process the JSON message that is returned from the server.

• Luckily Android provides classes to help, so we dont have to process the json string manually.

• Example code is presented below.


public void processJSON(String json){
try {
JSONArray all = new JSONArray(json);
for (int i=0; i<all.length(); i++){
JSONObject item=all.getJSONObject(i);
String name = item.getString("name");
String description = item.getString("description");
System.out.println(name + " - "+description);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

• The code above takes the JSON string and processes it as a JSON Array. Each item in the array
is then processed as a JSON object, with the properties of the object that we care about being
name and description.

• An example of how to use the class is shown below. In your case, you should use the string
returned from the server rather than a manually created string, and modify the processing code
to use the fields you care about rather than name and description.
String myJson="[{id: 1 , name : Pravesh , description : Java
Guy },{ id : 2 , name : Richard , description : C ++ Guy }]";
processJSON(myJson);

2 Practice
Try modifying your code to process the Cars that are returned from your PHP script from last week.
The code should display the license plate for each car on the screen, with one TextView created for
each car.

You might also like