Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. public class ItemData {
  2.  
  3. private String itemName;
  4. private String iconLink;
  5. private String iconLargeLink;
  6. private String type;
  7. private String typeIconLink;
  8. private String description;
  9. private boolean members;
  10. private int itemId;
  11. private int price;
  12.  
  13. public ItemData(int itemId) {
  14. try {
  15. URL url = new URL("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + itemId);
  16. URLConnection urlConnection = url.openConnection();
  17. BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
  18. StringBuilder stringBuilder = new StringBuilder();
  19. String inputLine;
  20. while ((inputLine = in.readLine()) != null) {
  21. stringBuilder.append(inputLine);
  22. }
  23. in.close();
  24. String jsonResult = stringBuilder.toString();
  25. if (jsonResult.contains("404 - Page not found")) {
  26. price = 0;
  27. this.itemId = itemId;
  28. } else {
  29. Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
  30. Matcher matcher = pattern.matcher(jsonResult);
  31. Map<String, String> groups = new HashMap<>();
  32. while (matcher.find()) {
  33. groups.put(matcher.group("key"), matcher.group("value"));
  34. }
  35. int beginIndex = jsonResult.indexOf("price\":") + 7;
  36. if (jsonResult.charAt(beginIndex) == '\"') {
  37. beginIndex++;
  38. }
  39. int endIndex = jsonResult.indexOf("\"", beginIndex);
  40. String priceStr = jsonResult.substring(beginIndex, endIndex);
  41. priceStr = priceStr.replace("k", "00").replace("m", "00000").replace(".", "").replace(",", "").replace("}", "");
  42. price = Integer.parseInt(priceStr);
  43. itemName = groups.get("name");
  44. iconLink = groups.get("icon");
  45. iconLargeLink = groups.get("icon_large");
  46. type = groups.get("type");
  47. typeIconLink = groups.get("typeIcon");
  48. description = groups.get("description");
  49. members = groups.get("members").equals("true");
  50. this.itemId = itemId;
  51. }
  52. } catch (IOException ignored) {
  53. System.out.println("Error loading item data with id: " + itemId);
  54. }
  55. }
  56.  
  57. public String getItemName() {
  58. return itemName;
  59. }
  60.  
  61. public String getIconLink() {
  62. return iconLink;
  63. }
  64.  
  65. public String getIconLargeLink() {
  66. return iconLargeLink;
  67. }
  68.  
  69. public String getType() {
  70. return type;
  71. }
  72.  
  73. public String getTypeIconLink() {
  74. return typeIconLink;
  75. }
  76.  
  77. public String getDescription() {
  78. return description;
  79. }
  80.  
  81. public int getItemId() {
  82. return itemId;
  83. }
  84.  
  85. public int getPrice() {
  86. return price;
  87. }
  88.  
  89. public boolean isMembers() {
  90. return members;
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement