private void testAddValue() { objUserTABLE.addValueUser("testUser", "testPass", "testName"); objFoodTABLE.addFood("ข้าวผัด", "60"); } // testAddValue
private void synJSONtoSQLite() { // Change Policy StrictMode.ThreadPolicy objPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(objPolicy); int intTime = 0; while (intTime <= 1) { // 1. Create InputStream InputStream objInputStream = null; String strJSON = ""; String strUserUrl = "http://swiftcodingthai.com/rest/php_get_data_master.php"; String strFoodUrl = "http://swiftcodingthai.com/rest/php_get_data_food.php"; HttpPost objHttpPost; try { HttpClient objHttpClient = new DefaultHttpClient(); if (intTime != 1) { objHttpPost = new HttpPost(strUserUrl); } else { objHttpPost = new HttpPost(strFoodUrl); } HttpResponse objHttpResponse = objHttpClient.execute(objHttpPost); HttpEntity objHttpEntity = objHttpResponse.getEntity(); objInputStream = objHttpEntity.getContent(); } catch (Exception e) { Log.d("rest", "InputStream ==> " + e.toString()); } // 2. Create JSON String try { BufferedReader objBufferedReader = new BufferedReader(new InputStreamReader(objInputStream, "UTF-8")); StringBuilder objStringBuilder = new StringBuilder(); String strLine = null; while ((strLine = objBufferedReader.readLine()) != null) { objStringBuilder.append(strLine); } // while objInputStream.close(); strJSON = objStringBuilder.toString(); } catch (Exception e) { Log.d("rest", "JSON ==> " + e.toString()); } // 3. Update SQLite try { final JSONArray objJsonArray = new JSONArray(strJSON); for (int i = 0; i < objJsonArray.length(); i++) { JSONObject myJsonObject = objJsonArray.getJSONObject(i); if (intTime != 1) { String strUser11 = myJsonObject.getString("User"); String strPassword11 = myJsonObject.getString("Password"); String strName11 = myJsonObject.getString("Name"); // Update UserTABLE objUserTABLE.addValueUser(strUser11, strPassword11, strName11); } else { String strFood = myJsonObject.getString("Food"); String strPrice = myJsonObject.getString("Price"); // Update FoodTABLE objFoodTABLE.addFood(strFood, strPrice); } } // for } catch (Exception e) { Log.d("rest", "SQLite update ==> " + e.toString()); } // Increase intTime intTime += 1; } // while } // synJSONtoSQLite