java - Gson from URL not filling up -
in spare time i'm making program fun , stumbled upon problem gson (probably dumb mistake i've been struggling hours now).
problem: response stays empty, screenshot debugging: https://gyazo.com/5c804dbfea24e386c285e36a153ef108
example response:
{ "response": { "total_count": 4, "games": [ { "appid": 730, "name": "counter-strike: global offensive", "playtime_2weeks": 4467, "playtime_forever": 322428, "img_icon_url": "69f7ebe2735c366c65c0b33dae00e12dc40edbe4", "img_logo_url": "d0595ff02f5c79fd19b06f4d6165c3fda2372820" } ] } }
code use (yes know key api invalid atm have key):
private static string readurl(string urlstring) throws exception { bufferedreader reader = null; try { url url = new url(urlstring); reader = new bufferedreader(new inputstreamreader(url.openstream())); stringbuffer buffer = new stringbuffer(); int read; char[] chars = new char[1024]; while ((read = reader.read(chars)) != -1) { buffer.append(chars, 0, read); } return buffer.tostring(); } { if (reader != null) { reader.close(); } } } static class item { int appid; string name; int playtime_2weeks; int playtime_forever; string img_icon_url; string img_logo_url; } static class response { int total_count; item[] games; } public static void main(string[] args) throws exception { string flusha = readurl("http://api.steampowered.com/iplayerservice/getrecentlyplayedgames/v0001/?key=xxxxxxxxxx&steamid=76561197991348083&count=1&format=json"); gson gson = new gson(); response response = gson.fromjson(flusha, response.class); system.out.print("flusha"); (item : response.games) system.out.println(" " + i.name + " played " + i.playtime_2weeks + " in last 2 weeks"); } }
well problem response stays empty.
you on right path.
see, json not correspond response
class. corresponds class:
class responsewrapper { response response; }
where response
class defined before.
Comments
Post a Comment