android - after parsing json null value is returned even though it is not null -
{ "page": 1, "results": [ { "poster_path": "/liv1qinfqz4dlp5u4lq6haiskoz.jpg", "adult": false, "overview": "under direction of ruthless instructor, talented young drummer begins pursue perfection @ cost, humanity.", "release_date": "2014-10-10", "genre_ids": [ 18, 10402 ], "id": 244786, "original_title": "whiplash", "original_language": "en", "title": "whiplash", "backdrop_path": "/6bbz6xyvgfjhqwbplnuh1lsj1ky.jpg", "popularity": 7.361171, "vote_count": 1949, "video": false, "vote_average": 8.32 } ] }
this json response when parse value of id null below networking , json parsing code please help..
public class movietask extends asynctask<string, void, list<movieobject>> { private string tag; private static final string base_string = "http://api.themoviedb.org/3/movie/"; private static final string api_key = "388e5684ea2f3bb8de279874cb6990a5"; private static final string movie_id="id"; private static final string movie_overview="overview"; private static final string movie_title="original_title"; private static final string movie_vote="vote_average"; private static final string movie_posterpath="poster_path"; private static final string movie_release_date="release_date"; private static final string movie_backdrop_path="backdrop_path"; // parse json data arraylist of movieobjects private list<movieobject> getparseddata(string forecastjsonstr) throws jsonexception { final string start_of_json = "results"; jsonobject startlist = new jsonobject(forecastjsonstr); jsonarray startarray = startlist.getjsonarray(start_of_json); int noofobjects = startarray.length(); list<movieobject> objectarray = new arraylist<>(); (int = 0; < noofobjects; i++) { jsonobject object = startarray.getjsonobject(i); movieobject finalobjectarray = new movieobject(); finalobjectarray.setmovieid(object.getstring(movie_id)); finalobjectarray.setoverview(object.getstring(movie_overview)); finalobjectarray.settitle(object.getstring(movie_title)); finalobjectarray.setmovierating(object.getstring(movie_vote)); finalobjectarray.setposterpath(object.getstring(movie_posterpath)); finalobjectarray.setreleasedate(object.getstring(movie_release_date)); finalobjectarray.setbackdroppath(object.getstring(movie_backdrop_path)+"/10"); objectarray.add(finalobjectarray); } return objectarray; } protected list<movieobject> doinbackground(string... params) { httpurlconnection urlconnection = null; bufferedreader reader = null; list<movieobject> finalobjectarray = new arraylist<>(); // contain raw json response string. string forecastjsonstr = null; try { url url = new url(base_string + params[0] + "?api_key="+api_key); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.connect(); // read input stream string inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if (inputstream == null) { // nothing do. } reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = reader.readline()) != null) { buffer.append(line + "\n"); } if (buffer.length() == 0) { return null; } forecastjsonstr = buffer.tostring(); log.i("hey", "hello" + forecastjsonstr); } catch (ioexception e) { log.e("placeholderfragment", "error ", e); } { if (urlconnection != null) { urlconnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final ioexception e) { log.e("placeholderfragment", "error closing stream", e); } } } try { finalobjectarray = getparseddata(forecastjsonstr); } catch (jsonexception e) { log.e("json exception", e.getmessage(), e); e.printstacktrace(); } return finalobjectarray; //final movieobject array } //onpostexecute method of asynctask protected void onpostexecute(list<movieobject> results) { } }
the id null
because "id" not string.
let's check json.
{ "page":1, "results":[ { "poster_path":"/liv1qinfqz4dlp5u4lq6haiskoz.jpg", "adult":false, "overview":"under direction of ruthless instructor, talented young drummer begins pursue perfection @ cost, humanity.", "release_date":"2014-10-10", "genre_ids":[ 18, 10402 ], "id":244786, <------- without "" = integer "original_title":"whiplash", <-------- "" = string "original_language":"en", "title":"whiplash", "backdrop_path":"/6bbz6xyvgfjhqwbplnuh1lsj1ky.jpg", "popularity":7.361171, "vote_count":1949, "video":false, "vote_average":8.32 } ] }
so, value use getint()
finalobjectarray.setmovieid(object.getint(movie_id).tostring());
Comments
Post a Comment