0

I'm trying to pull the "price" object from the "current" array, I have been at it for hours now with no luck, any help is appreciated! :)

try {
            URL url = new URL("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=2");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
                bufferedReader.close();
                return stringBuilder.toString();
            } finally {
                JSONArray nu1 = jobj.getJSONArray("current");
                JSONObject jobj = nu1.getJSONObject(0);
                String price = jobj.getString("price");
                Toast.makeText(getApplicationContext(), price, Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {
            Log.e("ERROR", e.getMessage(), e);
            return null;
        }
    }

    protected void onPostExecute(String response) {

    }
}

}

1
  • Welcome to Stack Overflow. Please read stackoverflow.com/help/how-to-ask on how to ask a good question. What have you tried so far? What does the data look like, and which elements are you trying to extract? Are you getting console errors at all? I reckon that the first time you use jobj is is undefined. Commented Aug 20, 2017 at 12:08

1 Answer 1

1

I tried to get response from your URL. here is the response :

{
"item": {
    "icon": "http://services.runescape.com/m=itemdb_rs/1502782993572_obj_sprite.gif?id=2",
    "icon_large": "http://services.runescape.com/m=itemdb_rs/1502782993572_obj_big.gif?id=2",
    "id": 2,
    "type": "Ammo",
    "typeIcon": "http://www.runescape.com/img/categories/Ammo",
    "name": "Cannonball",
    "description": "Ammo for the Dwarf Cannon.",
    "current": {
        "trend": "neutral",
        "price": 339
    },
    "today": {
        "trend": "positive",
        "price": "+1"
    },
    "members": "true",
    "day30": {
        "trend": "positive",
        "change": "+1.0%"
    },
    "day90": {
        "trend": "negative",
        "change": "-11.0%"
    },
    "day180": {
        "trend": "negative",
        "change": "-21.0%"
    }
}
}

there is no array in the response.

Edit:

assume that you store your response in a String named response, you can get price, using the following code:

        JSONObject json = new JSONObject(response);
        JSONObject item = json.getJSONObject("item");
        JSONObject current = item.getJSONObject("current");
        int price = current.getInt("price");

Edit2: use

String response =  stringBuilder.toString();

and then make a JSONObject from 'response' .

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, so how do I get the "price": 339 part to appear? I'm still no closer than before :(
I'm sorry I'm new to this stuff, only my second day haha :) I'm enjoying it all and just keen to get things working, thanks for your answer. I don't have a string named response, I'm editing a piece of code I found online to fit my needs, any help at all would be appreciated :D
@ElyJenkins please check Edit2 in my edited answer :)
@ElyJenkins if problem solved, please accept my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.