We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone --
SO. I’ve got this kind of working... but not really. I've had success with XML and JSON APIs (e.g., NOAA and Tumblr) but this one has me stumped. I’m trying to help a student access the Pokéapi (http://pokeapi.co/docsv2/) and we haven’t been able to get a result anything like the expected one. Here’s the code so far:
import http.requests.*;
JSONObject json;
void setup() {
size(600,600);
}
void draw() {
// actually returns something instead of throwing an error, which is progress
GetRequest get = new GetRequest("http://" + "pokeapi.co/api/v2/pokemon?id=11&limit=1");
get.send();
JSONObject json = parseJSONObject(get.getContent());
if (json == null) {
println("JSONObject could not be parsed");
} else {
println(json);
}
delay(30000); // don’t want to swamp the API
}
What I was expecting (or have been trying) to get as a result can be seen here: http://pokeapi.co/docsv1/#pokemon
{
"abilities": [
{
"name": "overgrow",
"resource_uri": "/api/v1/ability/1/"
},
{
"name": "chlorophyll",
"resource_uri": "/api/v1/ability/2/"
}
],
"attack": 49,
"catch_rate": 45,
"created": "2013-11-02T12:08:25.745455",
"defense": 49,
"egg_cycles": 21,
"egg_groups": [
{
"name": "Monster",
"resource_uri": "/api/v1/egg/1/"
},
{
"name": "Grass",
"resource_uri": "/api/v1/egg/8/"
}
],
"ev_yield": "1 Sp Atk",
"evolutions": {
"level": 16,
"method": "level up",
"resource_uri": "/api/v1/pokemon/2/",
"to": "Ivysaur"
},
"exp": 64,
"growth_rate": "ms",
"happiness": 70,
"height": "2'4",
"hp": 45,
"male_female_ratio": "87.5/12.5",
"modified": "2013-11-02T13:28:04.914889",
"moves": [
{
"learn_type": "other",
"name": "Tackle",
"resource_uri": "/api/v1/move/1/"
},
{
"learn_type": "other",
"name": "Growl",
"resource_uri": "/api/v1/move/2/"
},
{
"learn_type": "level up",
"level": 10,
"name": "Vine whip",
"resource_uri": "/api/v1/move/3/"
}
],
"name": "Bulbasaur",
"national_id": 1,
"resource_uri": "/api/v1/pokemon/4/",
"sp_atk": 65,
"sp_def": 65,
"species": "seed pokemon",
"speed": 45,
"total": 318,
"types": [
{
"name": "grass",
"resource_uri": "/api/v1/type/5/"
},
{
"name": "poison",
"resource_uri": "/api/v1/type/8/"
}
],
"weight": "15.2lbs"
}
Help? THANKS! :-)
Answers
https://processing.org/reference/loadJSONObject_.html
Load json object can take a URL, might be easier.
What are you expecting to get? That link in your previous post returns what is next below so it is working.
You can always copy and paste the link in your browser to see what the link returns.
Kf
Thank you both, but unfortunately....
@ koogs
I think I started there, it throws a null pointer exception (403): “java.io.IOException: Server returned HTTP response code: 403 for URL: http://pokeapi.co/api/v2/pokemon?id=11”
@kfrajer I was expecting something like the example response from the link (78 lines, as included in the original post). The response you posted (which is also the one I get, but which I forgot to post, D’OH) is only 14 lines and refers to the wrong number pokemon (#1 not #11). It ALWAYS returns the same number.
So... any more thoughts? Thanks again!
403 is http forbidden response.
Perhaps it is expecting some auth details, even something simple like a user agent string that a browser provides.
Or you could be hitting the daily limit on requests.
Am slightly confused because the json in your first post is from v1 but the request in the code is v2. So there's a mismatch there. V2 doesn't use request parameters but puts the ID in the path.
GET api/v2/pokemon/{id or name}
But that's for an individual Pokemon and I guess you need a list first to get the ids
Wow. Sometimes you think you’ve tried everything and then the answer turns out to be the most obvious... I REALLY thought I’d tried this, but apparently not.
It’s EXACTLY the same as the URL.
GetRequest get = new GetRequest("http://pokeapi.co/api/v2/pokemon/{id number}
Sorry to waste everyone’s time. :-\
Not sure if the v1 / v2 was the problem, but thanks to koogs’ prodding I found that the answer was there all along....