List of usage examples for org.json JSONObject getInt
public int getInt(String key) throws JSONException
From source file:com.jvoid.quote.controller.JVoidQuoteController.java
@RequestMapping(value = "quote/delete", method = RequestMethod.GET) public @ResponseBody String deleteCart(@RequestParam(required = false, value = "callback") String callback, @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) { // String jstr = "{\"cartId\":-1, \"productId\":2, \"attributeId\":1, \"quantity\":2}"; // String jstr = "{\"cartId\":1, \"productId\":3, \"attributeId\":1, \"quantity\":2}"; System.out.println("Inside delete"); int cartId = -1; int productId = -1; try {//from w w w . j av a2s . c om cartId = jsonParams.getInt("cartId"); productId = jsonParams.getInt("productId"); } catch (JSONException e) { e.printStackTrace(); } // cartId = 1; // productId = -1; CheckoutQuoteItem quoteItemToDelete = null; CheckoutQuote checkoutQuote = new CheckoutQuote(); List<CheckoutQuoteItem> quoteItemsList = null; if (productId != -1) { quoteItemToDelete = this.checkoutQuoteItemService.getCheckoutQuoteItem(cartId, productId); quoteItemsList = new ArrayList<CheckoutQuoteItem>(); quoteItemsList.add(quoteItemToDelete); } else { quoteItemsList = this.checkoutQuoteItemService.listCheckoutQuoteItems(cartId); System.out.println("ABHI Quote Item List Count= " + quoteItemsList.size()); } checkoutQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId); for (int i = 0; i < quoteItemsList.size(); i++) { CheckoutQuoteItem currentQuoteItemToDelete = quoteItemsList.get(i); checkoutQuote.setItemsCount(checkoutQuote.getItemsCount() - 1); checkoutQuote .setItemsQuantity(checkoutQuote.getItemsQuantity() - currentQuoteItemToDelete.getQuantity()); checkoutQuote .setBaseSubtotal(checkoutQuote.getBaseSubtotal() - currentQuoteItemToDelete.getBaseRowTotal()); checkoutQuote.setSubtotal(checkoutQuote.getSubtotal() - currentQuoteItemToDelete.getRowTotal()); this.checkoutQuoteItemService.removeCheckoutQuoteItem(currentQuoteItemToDelete.getId()); } checkoutQuote.setBaseGrandTotal(checkoutQuote.getBaseSubtotal() + 50); checkoutQuote.setGrandTotal(checkoutQuote.getSubtotal() + 50); checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime()); System.out.println("Abhi checkoutquote after = " + checkoutQuote.toString()); int returnCartId = cartId; if (productId == -1) { this.checkoutQuoteService.removeCheckoutQuote(checkoutQuote.getId()); returnCartId = -1; } else { this.checkoutQuoteService.addCheckoutQuote(checkoutQuote); } JSONObject jsonObj = new JSONObject(); try { jsonObj.put("cartId", returnCartId); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObj.toString(); }
From source file:com.jvoid.quote.controller.JVoidQuoteController.java
@RequestMapping(value = "quote/addaddress", method = RequestMethod.GET) public @ResponseBody String addAddress(@RequestParam(required = false, value = "callback") String callback, @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) { int cartId = -1; JSONObject user = null;//from ww w . ja va2 s.co m JSONObject billing = null; JSONObject shipping = null; String checkoutMethod = null; try { cartId = jsonParams.getInt("cartId"); user = jsonParams.getJSONObject("user"); billing = jsonParams.getJSONObject("billing"); shipping = jsonParams.getJSONObject("shipping"); checkoutMethod = jsonParams.getString("checkoutMethod"); } catch (JSONException e) { e.printStackTrace(); } CheckoutQuote checkoutQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId); checkoutQuote.setCheckoutMethod(checkoutMethod); checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime()); this.checkoutQuoteService.updateCheckoutQuote(checkoutQuote); try { CheckoutQuoteAddress checkoutQuoteAddress = new CheckoutQuoteAddress(); checkoutQuoteAddress.setQuoteId(cartId); checkoutQuoteAddress.setFirstname(user.getString("firstName")); checkoutQuoteAddress.setLastname(user.getString("lastName")); checkoutQuoteAddress.setCompany(user.getString("company")); checkoutQuoteAddress.setEmail(user.getString("emailAddress")); checkoutQuoteAddress.setAddressType("billing"); checkoutQuoteAddress .setStreet(billing.getString("address") + "," + billing.getString("streetAddress2")); checkoutQuoteAddress.setCity(billing.getString("city") + "," + billing.getString("state")); checkoutQuoteAddress.setCountryId(billing.getString("country")); checkoutQuoteAddress.setPostcode(billing.getString("zip")); checkoutQuoteAddress.setTelephone(billing.getString("telephone")); checkoutQuoteAddress.setFax(billing.getString("fax")); checkoutQuoteAddress.setCreatedAt(Utilities.getCurrentDateTime()); checkoutQuoteAddress.setUpdatedAt(Utilities.getCurrentDateTime()); this.checkoutQuoteAddressService.addCheckoutQuoteAddress(checkoutQuoteAddress); checkoutQuoteAddress = null; checkoutQuoteAddress = new CheckoutQuoteAddress(); checkoutQuoteAddress.setQuoteId(cartId); checkoutQuoteAddress.setFirstname(user.getString("firstName")); checkoutQuoteAddress.setLastname(user.getString("lastName")); checkoutQuoteAddress.setCompany(user.getString("company")); checkoutQuoteAddress.setEmail(user.getString("emailAddress")); checkoutQuoteAddress.setAddressType("shipping"); checkoutQuoteAddress .setStreet(shipping.getString("address") + "," + shipping.getString("streetAddress2")); checkoutQuoteAddress.setCity(shipping.getString("city") + "," + shipping.getString("state")); checkoutQuoteAddress.setCountryId(shipping.getString("country")); checkoutQuoteAddress.setPostcode(shipping.getString("zip")); checkoutQuoteAddress.setTelephone(shipping.getString("telephone")); checkoutQuoteAddress.setFax(shipping.getString("fax")); checkoutQuoteAddress.setCreatedAt(Utilities.getCurrentDateTime()); checkoutQuoteAddress.setUpdatedAt(Utilities.getCurrentDateTime()); this.checkoutQuoteAddressService.addCheckoutQuoteAddress(checkoutQuoteAddress); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject jsonObj = new JSONObject(); try { jsonObj.put("cartId", cartId); jsonObj.put("result", "Success"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObj.toString(); }
From source file:github.popeen.dsub.util.SongDBHandler.java
public void importData(JSONArray array) { SQLiteDatabase db = this.getReadableDatabase(); try {//ww w . jav a2 s.c om for (int i = 0; i < array.length(); i++) { JSONObject row = array.getJSONObject(i); ContentValues values = new ContentValues(); values.put(SONGS_ID, row.getInt("SONGS_ID")); values.put(SONGS_SERVER_KEY, row.getInt("SONGS_SERVER_KEY")); values.put(SONGS_SERVER_ID, row.getString("SONGS_SERVER_ID")); values.put(SONGS_COMPLETE_PATH, row.getString("SONGS_COMPLETE_PATH")); values.put(SONGS_LAST_PLAYED, row.getInt("SONGS_LAST_PLAYED")); values.put(SONGS_LAST_COMPLETED, row.getInt("SONGS_LAST_COMPLETED")); db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_REPLACE); } } catch (Exception e) { } }
From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java
public static RadioSong GetCurrentSongInformation(Context context, RadioRedditApplication application) { RadioSong radiosong = new RadioSong(); radiosong.ErrorMessage = ""; try {//from w ww .j a va 2s . c o m String status_url = context.getString(R.string.radio_reddit_base_url) + application.CurrentStream.Status + context.getString(R.string.radio_reddit_status); String outputStatus = ""; boolean errorGettingStatus = false; try { outputStatus = HTTPUtil.get(context, status_url); } catch (Exception ex) { ex.printStackTrace(); errorGettingStatus = true; // For now, not used. It is acceptable to error out and not alert the user // radiosong.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification); } if (!errorGettingStatus && outputStatus.length() > 0) { JSONTokener status_tokener = new JSONTokener(outputStatus); JSONObject status_json = new JSONObject(status_tokener); radiosong.Playlist = status_json.getString("playlist"); JSONObject songs = status_json.getJSONObject("songs"); JSONArray songs_array = songs.getJSONArray("song"); // get the first song in the array JSONObject song = songs_array.getJSONObject(0); radiosong.ID = song.getInt("id"); radiosong.Title = song.getString("title"); radiosong.Artist = song.getString("artist"); radiosong.Redditor = song.getString("redditor"); radiosong.Genre = song.getString("genre"); radiosong.CumulativeScore = song.getString("score"); if (radiosong.CumulativeScore.equals("{}")) radiosong.CumulativeScore = null; radiosong.Reddit_title = song.getString("reddit_title"); radiosong.Reddit_url = song.getString("reddit_url"); if (song.has("preview_url")) radiosong.Preview_url = song.getString("preview_url"); if (song.has("download_url")) radiosong.Download_url = song.getString("download_url"); if (song.has("bandcamp_link")) radiosong.Bandcamp_link = song.getString("bandcamp_link"); if (song.has("bandcamp_art")) radiosong.Bandcamp_art = song.getString("bandcamp_art"); if (song.has("itunes_link")) radiosong.Itunes_link = song.getString("itunes_link"); if (song.has("itunes_art")) radiosong.Itunes_art = song.getString("itunes_art"); if (song.has("itunes_price")) radiosong.Itunes_price = song.getString("itunes_price"); // get vote score String reddit_info_url = context.getString(R.string.reddit_link_by) + URLEncoder.encode(radiosong.Reddit_url); String outputRedditInfo = ""; boolean errorGettingRedditInfo = false; try { outputRedditInfo = HTTPUtil.get(context, reddit_info_url); } catch (Exception ex) { ex.printStackTrace(); errorGettingRedditInfo = true; // For now, not used. It is acceptable to error out and not alert the user // radiosong.ErrorMessage = "Unable to connect to reddit";//context.getString(R.string.error_RadioRedditServerIsDownNotification); } if (!errorGettingRedditInfo && outputRedditInfo.length() > 0) { // Log.e("radio_reddit_test", "Length: " + outputRedditInfo.length()); // Log.e("radio_reddit_test", "Value: " + outputRedditInfo); // TODO: sometimes the value contains "error: 404", need to check for that. (We can probably safely ignore this for now) JSONTokener reddit_info_tokener = new JSONTokener(outputRedditInfo); JSONObject reddit_info_json = new JSONObject(reddit_info_tokener); JSONObject data = reddit_info_json.getJSONObject("data"); // default value of score String score = context.getString(R.string.vote_to_submit_song); String likes = "null"; String name = ""; JSONArray children_array = data.getJSONArray("children"); // Song hasn't been submitted yet if (children_array.length() > 0) { JSONObject children = children_array.getJSONObject(0); JSONObject children_data = children.getJSONObject("data"); score = children_data.getString("score"); likes = children_data.getString("likes"); name = children_data.getString("name"); } radiosong.Score = score; radiosong.Likes = likes; radiosong.Name = name; } else { radiosong.Score = "?"; radiosong.Likes = "null"; radiosong.Name = ""; } return radiosong; } return null; } catch (Exception ex) { // We fail to get the current song information... CustomExceptionHandler ceh = new CustomExceptionHandler(context); ceh.sendEmail(ex); ex.printStackTrace(); radiosong.ErrorMessage = ex.toString(); return radiosong; } }
From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java
public static RadioEpisode GetCurrentEpisodeInformation(Context context, RadioRedditApplication application) { RadioEpisode radioepisode = new RadioEpisode(); radioepisode.ErrorMessage = ""; try {//from w ww . jav a 2 s.c o m String status_url = context.getString(R.string.radio_reddit_base_url) + application.CurrentStream.Status + context.getString(R.string.radio_reddit_status); String outputStatus = ""; boolean errorGettingStatus = false; try { outputStatus = HTTPUtil.get(context, status_url); } catch (Exception ex) { ex.printStackTrace(); errorGettingStatus = true; // For now, not used. It is acceptable to error out and not alert the user // radiosong.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification); } if (!errorGettingStatus && outputStatus.length() > 0) { JSONTokener status_tokener = new JSONTokener(outputStatus); JSONObject status_json = new JSONObject(status_tokener); radioepisode.Playlist = status_json.getString("playlist"); JSONObject episodes = status_json.getJSONObject("episodes"); JSONArray episodes_array = episodes.getJSONArray("episode"); // get the first episode in the array JSONObject song = episodes_array.getJSONObject(0); radioepisode.ID = song.getInt("id"); radioepisode.EpisodeTitle = song.getString("episode_title"); radioepisode.EpisodeDescription = song.getString("episode_description"); radioepisode.EpisodeKeywords = song.getString("episode_keywords"); radioepisode.ShowTitle = song.getString("show_title"); radioepisode.ShowHosts = song.getString("show_hosts").replaceAll(",", ", "); radioepisode.ShowRedditors = song.getString("show_redditors").replaceAll(",", ", "); radioepisode.ShowGenre = song.getString("show_genre"); radioepisode.ShowFeed = song.getString("show_feed"); radioepisode.Reddit_title = song.getString("reddit_title"); radioepisode.Reddit_url = song.getString("reddit_url"); if (song.has("preview_url")) radioepisode.Preview_url = song.getString("preview_url"); if (song.has("download_url")) radioepisode.Download_url = song.getString("download_url"); // get vote score String reddit_info_url = context.getString(R.string.reddit_link_by) + URLEncoder.encode(radioepisode.Reddit_url); String outputRedditInfo = ""; boolean errorGettingRedditInfo = false; try { outputRedditInfo = HTTPUtil.get(context, reddit_info_url); } catch (Exception ex) { ex.printStackTrace(); errorGettingRedditInfo = true; // For now, not used. It is acceptable to error out and not alert the user // radiosong.ErrorMessage = "Unable to connect to reddit";//context.getString(R.string.error_RadioRedditServerIsDownNotification); } if (!errorGettingRedditInfo && outputRedditInfo.length() > 0) { // Log.e("radio_reddit_test", "Length: " + outputRedditInfo.length()); // Log.e("radio_reddit_test", "Value: " + outputRedditInfo); // TODO: sometimes the value contains "error: 404", need to check for that (We can probably safely ignore this for now) JSONTokener reddit_info_tokener = new JSONTokener(outputRedditInfo); JSONObject reddit_info_json = new JSONObject(reddit_info_tokener); JSONObject data = reddit_info_json.getJSONObject("data"); // default value of score String score = context.getString(R.string.vote_to_submit_song); String likes = "null"; String name = ""; JSONArray children_array = data.getJSONArray("children"); // Episode hasn't been submitted yet if (children_array.length() > 0) { JSONObject children = children_array.getJSONObject(0); JSONObject children_data = children.getJSONObject("data"); score = children_data.getString("score"); likes = children_data.getString("likes"); name = children_data.getString("name"); } radioepisode.Score = score; radioepisode.Likes = likes; radioepisode.Name = name; } else { radioepisode.Score = "?"; radioepisode.Likes = "null"; radioepisode.Name = ""; } return radioepisode; } return null; } catch (Exception ex) { // We fail to get the current song information... CustomExceptionHandler ceh = new CustomExceptionHandler(context); ceh.sendEmail(ex); ex.printStackTrace(); radioepisode.ErrorMessage = ex.toString(); return radioepisode; } }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
public MCollection[] getCollections() { if (!isConnected()) { StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTIONS); return null; }// w w w .j ava 2 s .c o m MCollection[] collections = null; try { String strResponse = connect( "http://www.mendeley.com/oapi/library/collections?consumer_key=" + m_consumerkey); Log.i("MendeleyComm", strResponse); JSONArray jcols = new JSONArray(strResponse); collections = new MCollection[jcols.length()]; for (int i = 0; i < jcols.length(); i++) { JSONObject collection = jcols.getJSONObject(i); collections[i] = new MCollection(null); collections[i].id = collection.getString("id"); collections[i].name = collection.getString("name"); collections[i].type = collection.getString("type"); collections[i].size = collection.getInt("size"); } } catch (Exception e) { Log.e("MendeleyConnector", "Got exception when parsing online collection data"); Log.e("MendeleyConnector", e.getClass().getSimpleName() + ": " + e.getMessage()); } return collections; }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
public String[] getCollectionDocIDs(MCollection col, int page) { if (!isConnected()) { StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTION); return null; }// w w w . j a v a2s . co m String[] ids = null; try { String strResponse; if (col.m_coltype == MCollection.AUTHORED) { strResponse = connect("http://www.mendeley.com/oapi/library/documents/authored/?page=" + page + "&consumer_key=" + m_consumerkey); } else if (col.m_coltype == MCollection.LIBRARY) { strResponse = connect( "http://www.mendeley.com/oapi/library/?page=" + page + "&consumer_key=" + m_consumerkey); } else { strResponse = connect("http://www.mendeley.com/oapi/library/collections/" + col.id + "/?page=" + page + "&consumer_key=" + m_consumerkey); } Log.i("MendeleyComm", "Collection: " + col.id + "\n" + strResponse); JSONObject colData = new JSONObject(strResponse); JSONArray documents = colData.getJSONArray("document_ids"); ids = new String[documents.length()]; for (int i = 0; i < documents.length(); i++) { ids[i] = documents.getString(i); } // make sure we get all pages int totalpages = colData.getInt("total_pages"); if (page + 1 < totalpages) { String[] adddocs = getCollectionDocIDs(col, page + 1); String[] alldocs = new String[ids.length + adddocs.length]; for (int i = 0; i < ids.length; i++) alldocs[i] = ids[i]; for (int i = 0; i < adddocs.length; i++) alldocs[ids.length + i] = adddocs[i]; ids = alldocs; } } catch (Exception e) { Log.e("MendeleyConnector", "Got a " + e.getClass().getName() + ": " + e.getMessage()); } return ids; }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
public MGroup[] getGroups(MendeleyConnector con) { if (!isConnected()) { StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTIONS); return null; }/*w ww.j a va2 s.c om*/ MGroup[] groups = null; try { String strResponse = connect( "http://www.mendeley.com/oapi/library/groups?consumer_key=" + m_consumerkey); Log.i("MendeleyComm", strResponse); JSONArray items = new JSONArray(strResponse); groups = new MGroup[items.length()]; for (int i = 0; i < items.length(); i++) { JSONObject collection = items.getJSONObject(i); groups[i] = new MGroup(con); groups[i].id = collection.getString("id"); groups[i].name = collection.getString("name"); groups[i].type = collection.getString("type"); groups[i].size = collection.getInt("size"); } } catch (Exception e) { Log.e("MendeleyConnector", "Got exception when parsing online group data"); Log.e("MendeleyConnector", e.getClass().getSimpleName() + ": " + e.getMessage()); } return groups; }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
public String[] getGroupDocuments(MGroup group, int page) { if (!isConnected()) { StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTION); return null; }/* www . j a v a 2 s . c o m*/ String[] ids = null; try { String strResponse = connect("http://www.mendeley.com/oapi/library/groups/" + group.id + "/?page=" + page + "&consumer_key=" + m_consumerkey); Log.i("MendeleyComm", "Collection: " + group.id + "\n" + strResponse); JSONObject colData = new JSONObject(strResponse); JSONArray documents = colData.getJSONArray("document_ids"); ids = new String[documents.length()]; for (int i = 0; i < documents.length(); i++) { ids[i] = documents.getString(i); } // make sure we get all pages int totalpages = colData.getInt("total_pages"); if (page + 1 < totalpages) { String[] adddocs = getGroupDocuments(group, page + 1); String[] alldocs = new String[ids.length + adddocs.length]; for (int i = 0; i < ids.length; i++) alldocs[i] = ids[i]; for (int i = 0; i < adddocs.length; i++) alldocs[ids.length + i] = adddocs[i]; ids = alldocs; } } catch (Exception e) { Log.e("MendeleyConnector", "Got a " + e.getClass().getName() + ": " + e.getMessage()); } return ids; }
From source file:gmc.hotplate.util.JsonParser.java
public Recipe parseRecipeObject(JSONObject obj) { Recipe recipe = null;//from w w w. j a v a 2 s . c o m try { int recipeId = obj.getInt(TAG_RECIPE_ID); String recipeName = obj.getString(TAG_RECIPE_NAME); String recipeDescription = obj.getString(TAG_RECIPE_DESCRIPTION); int personCount = obj.getInt(TAG_RECIPE_PERSON); JSONArray jsonSteps = obj.getJSONArray(TAG_RECIPE_STEPS); List<Step> steps = new ArrayList<Step>(); for (int i = 0; i < jsonSteps.length(); i++) { Step step = parseStepObject(jsonSteps.getJSONObject(i)); if (step != null) { steps.add(step); } } List<Ingredient> ingredients = new ArrayList<Ingredient>(); JSONArray jsonIngredients = obj.getJSONArray(TAG_RECIPE_INGREDIENTS); for (int i = 0; i < jsonIngredients.length(); i++) { Ingredient ingredient = parseIngredientObject(jsonIngredients.getJSONObject(i)); if (ingredient != null) { ingredients.add(ingredient); } } List<String> categories = new ArrayList<String>(); if (obj.has(TAG_RECIPE_CATEGORIES)) { JSONArray jsonCategories = obj.getJSONArray(TAG_RECIPE_CATEGORIES); for (int i = 0; i < jsonCategories.length(); i++) { String tag = jsonCategories.getString(i); categories.add(tag); } } recipe = new Recipe(recipeId, recipeName, recipeDescription, personCount, steps); recipe.setIngredients(ingredients); recipe.setCategories(categories); } catch (JSONException e) { Log.w(LOG_TAG, "ParseRecipe() error: " + e.getMessage()); } return recipe; }