コード例 #1
0
    @Override
    protected Boolean doInBackground(String... params) {
      OAuth OAuth = new OAuth(context);
      Utils Utils = new Utils();

      Map<String, String> postParams = new HashMap<String, String>(1);
      postParams.put("client_id", Config.OAUTHCLIENTID);
      postParams.put("client_secret", Config.OAUTHCLIENTSECRET);
      JSONObject result = OAuth.call("StaticData", "", postParams);

      if (Config.DEBUG) Log.d("SyncStaticData", result.toString());

      try {
        // Save sync data
        JSONObject data = result.getJSONObject("data");

        JSONObject currentData;
        ActiveAndroid.beginTransaction();
        try {
          // Save countries
          new Delete().from(Country.class).execute();
          JSONArray countries = data.getJSONObject("country").getJSONArray("data");

          for (int i = 0; i < countries.length(); i++) {
            currentData = countries.getJSONObject(i);
            Country country = new Country();
            country.name = currentData.getString("name");
            country.countryId = currentData.getLong("id");
            country.save();
          }

          // Save cities
          new Delete().from(City.class).execute();
          JSONArray cities = data.getJSONObject("city").getJSONArray("data");

          for (int i = 0; i < cities.length(); i++) {
            currentData = cities.getJSONObject(i);
            City city = new City();
            city.name = currentData.getString("name");
            city.cityId = currentData.getLong("id");
            city.country_id = currentData.getLong("country_id");
            city.save();
          }

          ActiveAndroid.setTransactionSuccessful();
        } finally {
          ActiveAndroid.endTransaction();
        }
        return true;
      } catch (Exception e) {
        if (Config.DEBUG) Log.d("Sync", "Sync failed. Cause: " + e.toString());
        e.printStackTrace();
        return false;
      }
    }
コード例 #2
0
    @Override
    protected Boolean doInBackground(String... params) {
      OAuth OAuth = new OAuth(context);
      Utils Utils = new Utils();

      Map<String, String> postParams = new HashMap<String, String>(1);
      postParams.put("access_token", Utils.getToken(context));
      JSONObject result = OAuth.call("synchronize", "", postParams);

      if (Config.DEBUG) Log.d("Sync", result.toString());

      try {
        // Save sync data
        JSONObject data = result.getJSONObject("data");

        JSONObject currentData;
        ActiveAndroid.beginTransaction();
        try {

          // Save user data
          new Delete().from(User.class).execute();
          JSONObject userData = data.getJSONObject("user");
          Log.d("User", "User data > " + userData);
          User user = new User();
          user.username = userData.getString("username");
          user.name = userData.getString("name");
          if (userData.isNull("name")) {
            user.name = "";
          }
          String email = "";
          if (!userData.isNull("email")) {
            email = userData.getString("email");
          }
          user.email = email;

          String twitter = "";
          if (!userData.isNull("twittername")) {
            twitter = userData.getString("twittername");
          }
          user.twittername = twitter;

          String aboutme = "";
          if (!userData.isNull("aboutme")) {
            aboutme = userData.getString("aboutme");
          }
          user.aboutme = aboutme;
          user.cityId = userData.getLong("city_id");
          user.save();

          // Save countries
          new Delete().from(Country.class).execute();
          JSONArray countries = data.getJSONObject("country").getJSONArray("data");

          for (int i = 0; i < countries.length(); i++) {
            currentData = countries.getJSONObject(i);
            Country country = new Country();
            country.name = currentData.getString("name");
            country.countryId = currentData.getLong("id");
            country.save();
          }

          // Save cities
          new Delete().from(City.class).execute();
          JSONArray cities = data.getJSONObject("city").getJSONArray("data");

          for (int i = 0; i < cities.length(); i++) {
            currentData = cities.getJSONObject(i);
            City city = new City();
            city.name = currentData.getString("name");
            city.cityId = currentData.getLong("id");
            city.country_id = currentData.getLong("country_id");
            city.save();
          }

          // Save Lines
          new Delete().from(Line.class).execute();
          JSONArray lines = data.getJSONObject("line").getJSONArray("data");
          for (int i = 0; i < lines.length(); i++) {
            currentData = lines.getJSONObject(i);
            Line line = new Line();
            line.name = currentData.getString("name");
            // line.cityId = currentData.getLong("city_id");
            line.lineId = currentData.getLong("id");
            line.transport_id = currentData.getInt("transport_id");
            line.save();
          }

          // Save Transports
          new Delete().from(Transport.class).execute();
          JSONArray transports = data.getJSONObject("transport").getJSONArray("data");
          for (int i = 0; i < transports.length(); i++) {
            currentData = transports.getJSONObject(i);
            Transport transport = new Transport();
            transport.transportId = currentData.getLong("id");
            transport.name = currentData.getString("name");
            transport.icon = currentData.getString("icon");
            transport.save();
          }

          // Save station
          new Delete().from(Station.class).execute();
          JSONArray stations = data.getJSONObject("station").getJSONArray("data");
          for (int i = 0; i < stations.length(); i++) {
            currentData = stations.getJSONObject(i);
            Station station = new Station();
            station.name = currentData.getString("name");
            station.longitude = currentData.getDouble("longitude");
            station.stationId = currentData.getLong("id");
            station.latitude = currentData.getDouble("latitude");
            // station.lineId = currentData.getLong("line_id");
            station.save();
          }

          // Save CitiesTransports
          new Delete().from(CitiesTransport.class).execute();
          JSONArray citiesTransports = data.getJSONObject("citiestransport").getJSONArray("data");
          for (int i = 0; i < citiesTransports.length(); i++) {
            currentData = citiesTransports.getJSONObject(i);
            CitiesTransport citiesTransport = new CitiesTransport();
            citiesTransport.transportId = currentData.getLong("transport_id");
            citiesTransport.cityId = currentData.getLong("city_id");
            citiesTransport.save();
          }

          // Save LineStations
          new Delete().from(LinesStation.class).execute();
          JSONArray lineStations = data.getJSONObject("linesstations").getJSONArray("data");
          for (int i = 0; i < lineStations.length(); i++) {
            currentData = lineStations.getJSONObject(i);
            LinesStation linesStation = new LinesStation();
            linesStation.lineId = currentData.getLong("line_id");
            linesStation.stationId = currentData.getLong("station_id");
            linesStation.save();
          }

          ActiveAndroid.setTransactionSuccessful();
        } finally {
          ActiveAndroid.endTransaction();
        }
        return true;
      } catch (Exception e) {
        if (Config.DEBUG) Log.d("Sync", "Sync failed. Cause: " + e.toString());
        e.printStackTrace();
        return false;
      }
    }