Back to project page UTHPortal-Android-Gradle.
The source code is released under:
MIT License
If you think the Android project UTHPortal-Android-Gradle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uth.uthportal.buffers; //from www . ja v a 2s .c o m import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.uth.uthportal.collections.DayMenu; import com.uth.uthportal.collections.Dish; import com.uth.uthportal.collections.Food; /** * Parses a JSON object from a buffer, that contains information * about food schedule, received from the server. * Refer to the actual JSON file to get a better understanding of * the process. * @author GeorgeT * */ public class FoodParser { public Boolean wasSuccessful=true;//stays true if not changed public String errorMessage; public FoodParser(){} public Food parseFood(String JSONBuffer){ Date date ; List<DayMenu> weekMenu= new ArrayList<DayMenu>(); try { JSONObject food = new JSONObject(JSONBuffer); date = parseDateString(food.getString("date")); JSONArray weekDaysArray = food.getJSONArray("menu"); for(int i=0; i<weekDaysArray.length();i++){ weekMenu.add(parseDay(weekDaysArray.getJSONObject(i))); } } catch (JSONException e) { wasSuccessful = false; errorMessage = e.getMessage(); Log.e("e.fp.parseFood",errorMessage); return null; } catch (NullPointerException e) { wasSuccessful = false; errorMessage = e.getMessage(); Log.e("e.fp.parseFood:",errorMessage); return null; } if (date == null) return null; return new Food(date, weekMenu); } private DayMenu parseDay(JSONObject dayMenu){ String name; Date date; Dish dinner; Dish lunch; try { name = dayMenu.getString("name"); date = parseDateString(dayMenu.getString("date")); dinner = parseDish(dayMenu.getJSONObject("dinner")); lunch = parseDish(dayMenu.getJSONObject("lunch")); } catch (JSONException e) { wasSuccessful = false; errorMessage = e.getMessage(); Log.e("e.fp.parseFood:",errorMessage); return null; } if(date == null || dinner == null || lunch == null) return null; return new DayMenu(name, date, dinner, lunch); } private Dish parseDish(JSONObject dinnerLunch){ String desert; String main; String salad; try { desert = dinnerLunch.getString("desert"); main = dinnerLunch.getString("main"); salad = dinnerLunch.getString("salad"); } catch (JSONException e) { wasSuccessful = false; errorMessage = e.getMessage(); Log.e("e.fp.parseFood:",errorMessage); return null; } return new Dish(desert, main, salad); } private Date parseDateString(String dateStr){ //function to parse IsoDate SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date result; try { result = format.parse(dateStr); } catch (ParseException e) { wasSuccessful = false; errorMessage = e.getMessage(); Log.e("e.fp.parseDateString",errorMessage); return null; } return result; } }