Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ItemData {
- private String itemName;
- private String iconLink;
- private String iconLargeLink;
- private String type;
- private String typeIconLink;
- private String description;
- private boolean members;
- private int itemId;
- private int price;
- public ItemData(int itemId) {
- try {
- URL url = new URL("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + itemId);
- URLConnection urlConnection = url.openConnection();
- BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
- StringBuilder stringBuilder = new StringBuilder();
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- stringBuilder.append(inputLine);
- }
- in.close();
- String jsonResult = stringBuilder.toString();
- if (jsonResult.contains("404 - Page not found")) {
- price = 0;
- this.itemId = itemId;
- } else {
- Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
- Matcher matcher = pattern.matcher(jsonResult);
- Map<String, String> groups = new HashMap<>();
- while (matcher.find()) {
- groups.put(matcher.group("key"), matcher.group("value"));
- }
- int beginIndex = jsonResult.indexOf("price\":") + 7;
- if (jsonResult.charAt(beginIndex) == '\"') {
- beginIndex++;
- }
- int endIndex = jsonResult.indexOf("\"", beginIndex);
- String priceStr = jsonResult.substring(beginIndex, endIndex);
- priceStr = priceStr.replace("k", "00").replace("m", "00000").replace(".", "").replace(",", "").replace("}", "");
- price = Integer.parseInt(priceStr);
- itemName = groups.get("name");
- iconLink = groups.get("icon");
- iconLargeLink = groups.get("icon_large");
- type = groups.get("type");
- typeIconLink = groups.get("typeIcon");
- description = groups.get("description");
- members = groups.get("members").equals("true");
- this.itemId = itemId;
- }
- } catch (IOException ignored) {
- System.out.println("Error loading item data with id: " + itemId);
- }
- }
- public String getItemName() {
- return itemName;
- }
- public String getIconLink() {
- return iconLink;
- }
- public String getIconLargeLink() {
- return iconLargeLink;
- }
- public String getType() {
- return type;
- }
- public String getTypeIconLink() {
- return typeIconLink;
- }
- public String getDescription() {
- return description;
- }
- public int getItemId() {
- return itemId;
- }
- public int getPrice() {
- return price;
- }
- public boolean isMembers() {
- return members;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement