android - Image not loading with Picasso using Rx Observable and Retrofit2 -
i learning rxjava (observable) , retrofit2. landed in situation picasso not loading images url. details below:
public class intentkeys { public static final long interceptor_timeout = 15; public static final string popular_movies_service_endpoint = "http://api.themoviedb.org/3/"; public static final string popular_movies_poster_endpoint = "http://image.tmdb.org/t/p/w300/"; public static final string popular_movies_api_key = "key"; }
the recyclerview adapter:
public class gridviewadapter extends recyclerview.adapter<gridviewadapter.gridvieholder> { private static final string tag = gridviewadapter.class.getsimplename(); private static final int span_count = 2; private static final float thumbnail_ratio = 1.5f; private list<result> movies = new arraylist<>(); private context context; public gridviewadapter(list<result> movies, context context) { this.movies = movies; this.context = context; } @override public gridvieholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.movies_grid_view, parent, false); // view.getlayoutparams().height = (int) (parent.getwidth() / span_count * thumbnail_ratio); return new gridvieholder(view); } @override public void onbindviewholder(gridvieholder holder, int position) { gridvieholder posterholder = (gridvieholder) holder; posterholder.bind(movies.get(position)); /* result popularmovies = movies.get(position); string movieposter = popularmovies.getposterpath(); string movieposterurl = intentkeys.popular_movies_poster_endpoint + movieposter; picasso.with(context).load(movieposterurl.trim()) .config(bitmap.config.rgb_565) .into(posters);*/ } @override public int getitemcount() { return movies.size(); } public class gridvieholder extends recyclerview.viewholder { imageview posters; public gridvieholder(view itemview) { super(itemview); posters = (imageview) itemview.findviewbyid(r.id.movies_poster_grid_view); } public void bind(final result result) { uri uri = uri.parse(intentkeys.popular_movies_poster_endpoint).buildupon() .appendpath(imagesize.w185.getvalue()) .appendpath(result.getposterpath().replace("/", "")) .build(); picasso.with(itemview.getcontext()).load(uri).into(posters); } } }
below, how have defined service , retrofit manager:
public interface popularmoviesservice { @get("movie/popular") observable<result> getpopularmovies(@query("api_key") string apikey); } public class popularmoviesretrofitmanager { private popularmoviesservice service; private static retrofit retrofit = null; private static httplogginginterceptor getlogginginterceptor() { httplogginginterceptor interceptor = new httplogginginterceptor(); interceptor.setlevel(httplogginginterceptor.level.body); return interceptor; } private static okhttpclient getokhttpclient(httplogginginterceptor logginginterceptor) { return new okhttpclient.builder() .addinterceptor(logginginterceptor) .connecttimeout(intentkeys.interceptor_timeout, timeunit.seconds) .readtimeout(intentkeys.interceptor_timeout, timeunit.seconds) .build(); } public static retrofit getmoviesclient() { okhttpclient client = getokhttpclient(getlogginginterceptor()); if (retrofit == null) { retrofit = new retrofit.builder() .baseurl(intentkeys.popular_movies_service_endpoint) .addconverterfactory(gsonconverterfactory.create()) .addcalladapterfactory(rxjavacalladapterfactory.create()) .client(client) .build(); } return retrofit; } }
finally adapter , fragment classes. in activity class, have getsupportfragmentmanager()
load fragment.
public class gridviewadapter extends recyclerview.adapter<gridviewadapter.gridvieholder> { private static final string tag = gridviewadapter.class.getsimplename(); private static final int span_count = 2; private static final float thumbnail_ratio = 1.5f; private list<result> movies = new arraylist<>(); private context context; public gridviewadapter(list<result> movies, context context) { this.movies = movies; this.context = context; } @override public gridvieholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.movies_grid_view, parent, false); // view.getlayoutparams().height = (int) (parent.getwidth() / span_count * thumbnail_ratio); return new gridvieholder(view); } @override public void onbindviewholder(gridvieholder holder, int position) { gridvieholder posterholder = (gridvieholder) holder; posterholder.bind(movies.get(position)); /* result popularmovies = movies.get(position); string movieposter = popularmovies.getposterpath(); string movieposterurl = intentkeys.popular_movies_poster_endpoint + movieposter; picasso.with(context).load(movieposterurl.trim()) .config(bitmap.config.rgb_565) .into(posters);*/ } @override public int getitemcount() { return movies.size(); } public class gridvieholder extends recyclerview.viewholder { imageview posters; public gridvieholder(view itemview) { super(itemview); posters = (imageview) itemview.findviewbyid(r.id.movies_poster_grid_view); } public void bind(final result result) { uri uri = uri.parse(intentkeys.popular_movies_poster_endpoint).buildupon() .appendpath(imagesize.w185.getvalue()) .appendpath(result.getposterpath().replace("/", "")) .build(); picasso.with(itemview.getcontext()).load(uri).into(posters); } } } public class popularmoviesfragment extends fragment { private static final string tag = popularmoviesfragment.class.getcanonicalname(); private gridviewadapter gridviewadapter; private list<result> resultitems = new arraylist<>();; private subscription moviesubscription; private recyclerview recyclerview; private popularmoviesservice service = popularmoviesretrofitmanager.getmoviesclient().create(popularmoviesservice.class); public popularmoviesfragment() { } @override public void onattach(context context) { super.onattach(context); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_popular_movies, container, false); recyclerview = (recyclerview) view.findviewbyid(r.id.popular_movies_recycler_view); recyclerview.sethasfixedsize(true); recyclerview.layoutmanager layoutmanager = new gridlayoutmanager(getcontext(), 2); recyclerview.setlayoutmanager(layoutmanager); gridviewadapter = new gridviewadapter(resultitems, getcontext()); recyclerview.setadapter(gridviewadapter); return (view); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sethasoptionsmenu(true); } @override public void onstart() { super.onstart(); getmoviessubscription(); } private void getmoviessubscription() { moviesubscription = service.getpopularmovies(intentkeys.popular_movies_api_key) .subscribeon(schedulers.io()) .observeon(androidschedulers.mainthread()) .subscribe( new action1<result>() { @override public void call(result popularmovies) { } }, new action1<throwable>() { @override public void call(throwable throwable) { } }, new action0() { @override public void call() { displayposters(); log.i(tag, "getmoviessubscription: completed"); } } ); } @override public void ondestroyview() { super.ondestroyview(); if (moviesubscription != null && moviesubscription.isunsubscribed()) { moviesubscription.unsubscribe(); } } private void displayposters() { gridviewadapter = new gridviewadapter(resultitems, getcontext()); recyclerview.setadapter(gridviewadapter); recyclerview.invalidate(); } }
the problem is, picasso not loading images in gridview api call. here highly appreciated.
here json format:
{ "page":1, "results":[ { "poster_path":"\/5n20rqurev5cndcmjhvuzhpocnc.jpg", "adult":false, "overview":"following events of age of ultron, collective governments of world pass act designed regulate superhuman activity. polarizes opinion amongst avengers, causing 2 factions side iron man or captain america, causes epic battle between former allies.", "release_date":"2016-04-27", "genre_ids":[ 28, 53, 878 ], "id":271110, "original_title":"captain america: civil war", "original_language":"en", "title":"captain america: civil war", "backdrop_path":"\/rqahkvxldb9thlnbqdwozri0yvd.jpg", "popularity":49.819238, "vote_count":2787, "video":false, "vote_average":6.93 }, { "poster_path":"\/e1mjopzas2knsvpbpahq1a6sksn.jpg",
here, poster_path
should getting images from.
at last, have following console output. see getting response , 200. as, data in response. below console output.
thanks in advance support getting resolve problem.
sorry, forgot layout: movies_grid_view.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:orientation="vertical"> <imageview android:id="@+id/movies_poster_grid_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
here log:
d: classloader referenced unknown path: /data/app/com.example.mchapagai-2/lib/x86_64 w: classloader referenced unknown path: /data/app/com.example.mchapagai-2/lib/x86_64 w: before android 4.1, method android.graphics.porterduffcolorfilter android.support.graphics.drawable.vectordrawablecompat.updatetintfilter(android.graphics.porterduffcolorfilter, android.content.res.colorstatelist, android.graphics.porterduff$mode) have incorrectly overridden package-private method in android.graphics.drawable.drawable d: --> http://api.themoviedb.org/3/discover/movie?api_key=api_key http/1.1 d: --> end d: use egl_swap_behavior_preserved: true [ 09-13 04:50:41.940 9654: 9654 d/ ] hostconnection::get() new host connection established 0x7f16976bf100, tid 9654 d: <-- 200 ok http://api.themoviedb.org/3/discover/movie?api_key=api_key (239ms) d: access-control-allow-origin: * d: cache-control: public, max-age=21600 d: content-type: application/json;charset=utf-8 d: date: tue, 13 sep 2016 15:17:15 gmt d: server: openresty d: vary: accept-encoding d: x-ratelimit-limit: 40 d: x-ratelimit-remaining: 39 d: x-ratelimit-reset: 1473779845 d: connection: keep-alive d: {"page":1,"results":[{"poster_path":"\/5n20rqurev5cndcmjhvuzhpocnc.jpg","adult":false,"overview":"following events of age of ultron, collective governments of world pass act designed regulate superhuman activity. polarizes opinion amongst avengers, causing 2 factions side iron man or captain america, causes epic battle between former allies.","release_date":"2016-04-27","genre_ids":[28,53,878],"id":271110,"original_title":"captain america: civil war","original_language":"en","title":"captain america: civil war","backdrop_path":"\/rqahkvxldb9thlnbqdwozri0yvd.jpg","popularity":48.757871,"vote_count":2812,"video":false,"vote_average":6.92},{"poster_path":"\/e1mjopzas2knsvpbpahq1a6sksn.jpg","adult":false,"overview":"from dc comics comes suicide squad, antihero team of incarcerated supervillains act deniable assets united states government, undertaking high-risk black ops missions in exchange commuted prison sentences.","release_date":"2016-08-03","genre_ids":[28,80,14],"id":297761,"original_title":"suicide squad","original_language":"en","title":"suicide squad","backdrop_path":"\/ndlq2cuc3cjtl7ltynw6i4bop4s.jpg","popularity":31.257563,"vote_count":1805,"video":false,"vote_average":5.9},{"poster_path":"\/zsouwwrysxshpct4t3ukcqgayyo.jpg","adult":false,"overview":"after re-emergence of world's first mutant, world-destroyer apocalypse, x-men must unite defeat extinction level plan.","release_date":"2016-05-18","genre_ids":[28,12,14,878],"id":246655,"original_title":"x-men: apocalypse","original_language":"en","title":"x-men: apocalypse","backdrop_path":"\/oqwwth5aotbwg9o8scavigcaded.jpg","popularity":25.105112,"vote_count":1737,"video":false,"vote_average":6.1},{"poster_path":"\/tgfrdjs5pfw20aoh1orezuxw8cn.jpg","adult":false,"overview":"arthur bishop thought had put murderous past behind him when formidable foe kidnaps love of life. forced travel globe complete 3 impossible assassinations, , best, make them accidents.","release_date":"2016-08-25","genre_ids":[80,28,53],"id":278924,"original_title":"mechanic: resurrection","original_language":"en","title":"mechanic: resurrection","backdrop_path":"\/3orhlbxmlbxhfmqusx1emwqiuq3.jpg","popularity":23.569722,"vote_count":249,"video":false,"vote_average":4.45},{"poster_path":"\/h28t2jnngrzx0fiuaw8ahqfhixr.jpg","adult":false,"overview":"a cheated on married woman falls younger man has moved in next door, torrid affair takes dangerous turn.","release_date":"2015-01-23","genre_ids":[53],"id":241251,"original_title":"the boy next door","original_language":"en","title":"the boy next door","backdrop_path":"\/vj4ihmh4hcmzyyjtmiybybtwr5o.jpg","popularity":23.27805,"vote_count":710,"video":false,"vote_average":4.03},{"poster_path":"\/5bcpxplyp4wgctribpz6wtnkvj5.jpg","adult":false,"overview":"after supervillain shredder escapes custody, joins forces mad scientist baxter stockman , 2 dimwitted henchmen, bebop , rocksteady, unleash diabolical plan take on world. turtles prepare take on shredder , new crew, find facing greater evil similar intentions: notorious krang.","release_date":"2016-06-01","genre_ids":[28,12,35,878],"id":308531,"original_title":"teenage mutant ninja turtles: out of shadows","original_language":"en","title":"teenage mutant ninja turtles: out of shadows","backdrop_path":"\/999ruhzvog8ocyvcccvv9ygmmjl.jpg","popularity":19.58401,"vote_count":285,"video":false,"vote_average":5.45},{"poster_path":"\/lw0iqosmsqcy1qnvieidpplmnwk.jpg","adult":false,"overview":"a group of teens break blind man's home thinking they'll away perfect crime. they're wrong.","release_date":"2016-08-25","genre_ids":[27,53],"id":300669,"original_title":"don't breathe","original_language":"en","title":"don't b d: reathe","backdrop_path":"\/bcthhxq3alldu3kfst0rc8mtan5.jpg","popularity":17.777405,"vote_count":120,"video":false,"vote_average":5.99},{"poster_path":"\/hu0e130tsgdsya4k9lc3xrn5wyt.jpg","adult":false,"overview":"one year after outwitting fbi , winning public’s adulation mind-bending spectacles, 4 horsemen resurface find face face new enemy enlists them pull off dangerous heist yet.","release_date":"2016-06-02","genre_ids":[28,12,35,80,9648,53],"id":291805,"original_title":"now see me 2","original_language":"en","title":"now see me 2","backdrop_path":"\/zrao2ooa6s6dqmq7zsubdyibrap.jpg","popularity":17.576621,"vote_count":861,"video":false,"vote_average":6.63},{"poster_path":"\/kqjl17yufvn9ovlyxypvtyrffak.jpg","adult":false,"overview":"an apocalyptic story set in furthest reaches of our planet, in stark desert landscape humanity broken, , crazed fighting necessities of life. within world exist 2 rebels on run might able restore order. there's max, man of action , man of few words, seeks peace of mind following loss of wife , child in aftermath of chaos. , furiosa, woman of action , woman believes path survival may achieved if can make across desert childhood homeland.","release_date":"2015-05-13","genre_ids":[28,12,878,53],"id":76341,"original_title":"mad max: fury road","original_language":"en","title":"mad max: fury road","backdrop_path":"\/tbhdm8ujab4victsulyfl3lxmcd.jpg","popularity":15.943655,"vote_count":5310,"video":false,"vote_average":7.25},{"poster_path":"\/lfssltlfozwpaglo31ooueirbgq.jpg","adult":false,"overview":"the dangerous former operative of cia drawn out of hiding uncover hidden truths past.","release_date":"2016-07-27","genre_ids":[28,53],"id":324668,"original_title":"jason bourne","original_language":"en","title":"jason bourne","backdrop_path":"\/aot2yrjujlg5vke3imolvhltd3m.jpg","popularity":15.732751,"vote_count":824,"video":false,"vote_average":5.39},{"poster_path":"\/6vuxwcfbejpfujmxrpgk0anmvfq.jpg","adult":false,"overview":"an injured surfer stranded on buoy needs shore, great white shark stalking might have other ideas.","release_date":"2016-06-24","genre_ids":[27,18,53],"id":332567,"original_title":"the shallows","original_language":"en","title":"the shallows","backdrop_path":"\/lekhdk4g0naktmchbtsmc1on3o1.jpg","popularity":15.369931,"vote_count":273,"video":false,"vote_average":6.13},{"poster_path":"\/nbnzadxqjsdt05shlqgt0huc5gm.jpg","adult":false,"overview":"interstellar chronicles adventures of group of explorers make use of newly discovered wormhole surpass limitations on human space travel , conquer vast distances involved in interstellar voyage.","release_date":"2014-11-05","genre_ids":[12,18,878],"id":157336,"original_title":"interstellar","original_language":"en","title":"interstellar","backdrop_path":"\/xu9zaaevzq5nnrsxn6jcahlng4i.jpg","popularity":15.24346,"vote_count":5678,"video":false,"vote_average":8.11},{"poster_path":"\/y31qb9kn3xsuda15tv7uwq9xluw.jpg","adult":false,"overview":"light years earth, 26 years after being abducted, peter quill finds himself prime target of manhunt after discovering orb wanted ronan accuser.","release_date":"2014-07-30","genre_ids":[28,878,12],"id":118340,"original_title":"guardians of galaxy","original_language":"en","title":"guardians of galaxy","backdrop_path":"\/bharw8xrmqeqf3t8hpumy7zok4x.jpg","popularity":14.648276,"vote_count":5075,"video":false,"vote_average":7.96},{"poster_path":"\/jjbgi2r5crt36xf6inuehzscecb.jpg","adult":false,"overview":"twenty-two years after events of jurassic park, isla nublar features functioning dinosaur theme park, jurassic world, envisioned john hammond.","release_ d: date":"2015-06-09","genre_ids":[28,12,878,53],"id":135397,"original_title":"jurassic world","original_language":"en","title":"jurassic world","backdrop_path":"\/dkmd5qlogermieixc4ynpuvax2t.jpg","popularity":14.280376,"vote_count":4987,"video":false,"vote_average":6.59},{"poster_path":"\/ckrtpz6fz35l5ybjqvklwzzslo7.jpg","adult":false,"overview":"the peaceful realm of azeroth stands on brink of war civilization faces fearsome race of invaders: orc warriors fleeing dying home colonize another. portal opens connect 2 worlds, 1 army faces destruction , other faces extinction. opposing sides, 2 heroes set on collision course decide fate of family, people, , home.","release_date":"2016-05-25","genre_ids":[28,12,14],"id":68735,"original_title":"warcraft","original_language":"en","title":"warcraft","backdrop_path":"\/5sx2rgkxz7nvmajr5z5lprqsxka.jpg","popularity":13.977208,"vote_count":741,"video":false,"vote_average":6.13},{"poster_path":"\/voipe2myi26udwp978hsyornuwc.jpg","adult":false,"overview":"after threat tiger shere khan forces him flee jungle, man-cub named mowgli embarks on journey of self discovery of panther, bagheera, , free spirited bear, baloo.","release_date":"2016-04-07","genre_ids":[12,18,14],"id":278927,"original_title":"the jungle book","original_language":"en","title":"the jungle book","backdrop_path":"\/eiotsgg9fcvrbc4r2nxav61jf4f.jpg","popularity":13.822341,"vote_count":1211,"video":false,"vote_average":6.35},{"poster_path":"\/gj282pniaa78zjfbaixylxnxedi.jpg","adult":false,"overview":"katniss everdeen reluctantly becomes symbol of mass rebellion against autocratic capitol.","release_date":"2014-11-18","genre_ids":[878,12,53],"id":131631,"original_title":"the hunger games: mockingjay - part 1","original_language":"en","title":"the hunger games: mockingjay - part 1","backdrop_path":"\/83nhcz2kcneppxy50ky2vldewjj.jpg","popularity":13.696672,"vote_count":3218,"video":false,"vote_average":6.69},{"poster_path":"\/cgopbv9wa5geejkun892jrveart.jpg","adult":false,"overview":"fearing actions of god-like super hero left unchecked, gotham city’s own formidable, forceful vigilante takes on metropolis’s revered, modern-day savior, while world wrestles sort of hero needs. , batman , superman @ war 1 another, new threat arises, putting mankind in greater danger it’s ever known before.","release_date":"2016-03-23","genre_ids":[28,12,14],"id":209112,"original_title":"batman v superman: dawn of justice","original_language":"en","title":"batman v superman: dawn of justice","backdrop_path":"\/vsjbempztyb7ynsyy56xyxifaqz.jpg","popularity":13.659184,"vote_count":3614,"video":false,"vote_average":5.52},{"poster_path":"\/invq3frqcyirl2la8izikyyxfnr.jpg","adult":false,"overview":"based upon marvel comics’ unconventional anti-hero, deadpool tells origin story of former special forces operative turned mercenary wade wilson, after being subjected rogue experiment leaves him accelerated healing powers, adopts alter ego deadpool. armed new abilities , dark, twisted sense of humor, deadpool hunts down man destroyed life.","release_date":"2016-02-09","genre_ids":[28,12,35,10749],"id":293660,"original_title":"deadpool","original_language":"en","title":"deadpool","backdrop_path":"\/nbirdhotupdd9hkdbry02a8vhpv.jpg","popularity":13.637572,"vote_count":4967,"video":false,"vote_average":7.17},{"poster_path":"\/odl2ryj0sv2bmjgshvgjb3qzvwp.jpg","adult":false,"overview":"the city needs heroes. darkness has settled on new york city shredder , evil foot clan have iron grip on police politicians. future grim until 4 unlikely outcast brothers rise sewers , discover destiny teenage mutant ninja turtles. turtles must work w d: ith fearless reporter april , wise-cracking cameraman vern fenwick save city , unravel shredder's diabolical plan.","release_date":"2014-08-07","genre_ids":[878,28,12,14,35],"id":98566,"original_title":"teenage mutant ninja turtles","original_language":"en","title":"teenage mutant ninja turtles","backdrop_path":"\/oqcxgt5nl1chpeotxcdvxlle6p.jpg","popularity":13.480196,"vote_count":1660,"video":false,"vote_average":5.9}],"total_results":279563,"total_pages":13979} d: <-- end http (12492-byte body) i: getmoviessubscription: completed [ 09-13 04:50:42.309 9654: 9699 d/ ] hostconnection::get() new host connection established 0x7f16a00bffc0, tid 9699 i: initialized egl, version 1.4 w: setting airplane_mode_on has moved android.provider.settings.system android.provider.settings.global, returning read-only value. d: poster url: http://image.tmdb.org/t/p/w185/null d: dispatcher enqueued [r0]+108ms d: hunter executing [r0]+156ms d: dispatcher batched [r0]+320ms error d: dispatcher delivered [r0]+560ms d: main errored [r0]+560ms
i think problem uri passed picasso
uri uri = uri.parse(intentkeys.popular_movies_poster_endpoint).buildupon() .appendpath(imagesize.w185.getvalue()) .appendpath(result.getposterpath().replace("/", "")) .build();
it should have key query parameter.
Comments
Post a Comment