Android Open Source - UTHPortal-Android-Gradle Food Parser






From Project

Back to project page UTHPortal-Android-Gradle.

License

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.

Java Source Code

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




Java Source Code List

com.uth.uthportal.AboutScreen.java
com.uth.uthportal.CoursesFragment.java
com.uth.uthportal.DepartmentFragment.java
com.uth.uthportal.FoodFragment.java
com.uth.uthportal.MainScreen.java
com.uth.uthportal.SettingsScreen.java
com.uth.uthportal.adapter.AdapterManager.java
com.uth.uthportal.adapter.AdapterProvider.java
com.uth.uthportal.adapter.ExpandableListAdapter.java
com.uth.uthportal.adapter.SettingsAdapter.java
com.uth.uthportal.adapter.TabsPagerAdapter.java
com.uth.uthportal.buffers.AvailableCoursesParser.java
com.uth.uthportal.buffers.CoursesParser.java
com.uth.uthportal.buffers.FileOperation.java
com.uth.uthportal.buffers.FoodParser.java
com.uth.uthportal.buffers.GeneralAnnParser.java
com.uth.uthportal.buffers.SettingsManager.java
com.uth.uthportal.collections.AnnItem.java
com.uth.uthportal.collections.Announcements.java
com.uth.uthportal.collections.AvailableCourse.java
com.uth.uthportal.collections.CourseInfo.java
com.uth.uthportal.collections.Course.java
com.uth.uthportal.collections.DayMenu.java
com.uth.uthportal.collections.DefaultIntervals.java
com.uth.uthportal.collections.Dish.java
com.uth.uthportal.collections.Food.java
com.uth.uthportal.collections.GeneralAnnouncement.java
com.uth.uthportal.collections.Settings.java
com.uth.uthportal.network.ApiLinks.java
com.uth.uthportal.network.AppRater.java
com.uth.uthportal.network.AsyncJSONDownloader.java
com.uth.uthportal.network.JSONDownloader.java
com.uth.uthportal.service.DataSyncService.java
com.uth.uthportal.util.SystemUiHiderBase.java
com.uth.uthportal.util.SystemUiHiderHoneycomb.java
com.uth.uthportal.util.SystemUiHider.java