After scripting the database through PHP, the next step would be to parse the data from the database to be viewable to the app. This can be through two main methods; either by parsing the data through XML, or else through JSON. Below I will be discussing the the pros and cons of both methods, and then give reasons why I chose to parse the data through JSON over XML.
XML Parsing
XML parsing in android is done using the DOM parser. The parser class mainly deals the following operations:
- Getting XML content by making HTTP request.
- Parsing XML content and getting DOM element of xml.
- Get each xml child element value by passing element node name.
The xml document is parsed by making http requests through Android code. A typical http request would be the following:
public
String getXmlFromUrl(String url) {
String xml =
null
;
try
{
// defaultHttpClient
DefaultHttpClient httpClient =
new
DefaultHttpClient();
HttpPost httpPost =
new
HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
// return XML
return
xml;
}
{ "Quote": [ { "quoteID": "1", "quoteName": "For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.", "quoteAuthor": "John 3:16" } ], "success": 1 }
The above is the data parsed from the Quote table in the database.
In general all the JSON nodes will start with a square bracket or with a curly bracket. The difference between [ and { is, the square bracket ([) represents starting of an JSONArray node whereas curly bracket ({) represents JSONObject. So while accessing these nodes we need to call appropriate method to access the data.
The following is the code for parsing the data using the JSONParser class in Android:
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); Log.e("JSON", json); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; }
Finally, the app will show the data in the following doInBackground method:
protected String doInBackground(String... args) { List<NameValuePair> params = new ArrayList<NameValuePair>(); JSONObject json = jParser.getJSONFromUrl(url_quote_details,"GET", params); try { int success = json.getInt(CONNECTION_STATUS); if (success == 1) { Inspiration = json.getJSONArray(TABLE_QUOTE); for (int i = 0; i < Inspiration.length(); i++) { JSONObject insp = Inspiration.getJSONObject(i); name = insp.getString(COL_DESC); author = insp.getString(COL_AUTHOR); conCat = name + "_" + author; } } else { pDialog.dismiss(); } } catch (JSONException e) { pDialog.dismiss(); e.printStackTrace(); } return conCat; }
The app shows the data in the following method:
protected String doInBackground(String... args) { List<NameValuePair> params = new ArrayList<NameValuePair>(); JSONObject json = jParser.getJSONFromUrl(url_quote_details,"GET", params); try { int success = json.getInt(CONNECTION_STATUS); if (success == 1) { Inspiration = json.getJSONArray(TABLE_QUOTE); for (int i = 0; i < Inspiration.length(); i++) { JSONObject insp = Inspiration.getJSONObject(i); name = insp.getString(COL_DESC); author = insp.getString(COL_AUTHOR); conCat = name + "_" + author; } } else { pDialog.dismiss(); } } catch (JSONException e) { pDialog.dismiss(); e.printStackTrace(); } return conCat; }
Below is how the parsed text would look in the App: