java - Android - get image from JSON using Picasso -
i'm learning creating news app in android studio json , far app working except when fetching image. there wrong codes , can me it?
i used these in manifest:
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.read_external_storage"/> <uses-permission android:name="android.permission.access_wifi_state"/> i used in gradle
compile 'com.squareup.picasso:picasso:2.5.2' and here's java
detail.java
import... public class detail extends activity{ public imageloader imageloader;{ imageloader = new imageloader(null); } jsonarray string_json = null; string idkbj; private progressdialog progressdialog; jsonparser jsonparser = new jsonparser(); public static final string tag_id = "id"; public static final string tag_cat = "category"; public static final string tag_title = "title"; public static final string tag_author = "author"; public static final string tag_date = "date"; public static final string tag_con = "content"; public static final string tag_img = "featured_image"; public static final string tag_date_cre = "date_created"; private static final string url_kbj = "https://api.url_api_here"; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.single_list_item); intent = getintent(); idkbj = i.getstringextra(tag_id); toast.maketext(getapplicationcontext(), "id berita = " + idkbj, toast.length_short).show(); new getdetail().execute(); } class getdetail extends asynctask<string,string, string>{ @override protected void onpreexecute(){ super.onpreexecute(); progressdialog = new progressdialog(detail.this); progressdialog.setmessage("loading..."); progressdialog.setindeterminate(false); progressdialog.setcancelable(true); progressdialog.show(); } protected string doinbackground(string... params){ try{ list<namevaluepair> params1 = new arraylist<>(); params1.add(new basicnamevaluepair("id", idkbj)); final jsonobject json = jsonparser.makehttprequest(url_kbj + "/" + idkbj + "/", "get", params1); //log.i("jsonda =", string.valueof(json)); final jsonobject data = json.getjsonobject("data"); //log.e("json2 =", string.valueof(data)); runonuithread(new runnable() { @override public void run() { imageview thumb_img = (imageview) findviewbyid(r.id.featured_image); textview title = (textview) findviewbyid(r.id.title); //textview id = (textview) findviewbyid(r.id.id); textview category = (textview) findviewbyid(r.id.category); textview author = (textview) findviewbyid(r.id.author); textview date = (textview) findviewbyid(r.id.date); textview content = (textview) findviewbyid(r.id.content); try{ string cat_d = data.getstring(tag_cat); string tit_d = data.getstring(tag_title); string aut_d = data.getstring(tag_author); string con_d = data.getstring(tag_con); string url_detail_img = data.getstring(tag_img); jsonobject date_cre = data.getjsonobject(tag_date_cre); string date_d = date_cre.getstring(tag_date); category.settext(cat_d); title.settext(tit_d); author.settext(aut_d); content.settext(con_d); date.settext(date_d); picasso.with(getapplicationcontext()) .load(url_detail_img) .error(r.drawable.temp_img) .into(thumb_img); log.d("detail image = ", url_detail_img); } catch (jsonexception e){ e.printstacktrace(); } catch (exception e){ e.printstacktrace(); } } }); } catch (jsonexception e){ e.printstacktrace(); } return null; } protected void onpostexecute(string file_url){ progressdialog.dismiss(); } } } json object example in onclickitem want get:
{ "data": { "id": 3255, "permalink": "http://kawaiibeautyjapan.com/article/3255/5-tips-diet-mudah-yang-bisa-kamu-lakukan-bersama-pasanganmu", "slug": "5-tips-diet-mudah-yang-bisa-kamu-lakukan-bersama-pasanganmu", "title": "5 tips diet mudah yang bisa kamu lakukan bersama pasanganmu", "featured_image": "http://kawaiibeautyjapan.com/upload/article/pc/article_3255.jpg"} }
when try debug it, ends
edit
i sort of find problem.
in "featured_image" object, url using http.
but when tried click it, turns https
is there way change this?
import... public class detail extends activity{ public imageloader imageloader;{ imageloader = new imageloader(null); } jsonarray string_json = null; string idkbj; private progressdialog progressdialog; jsonparser jsonparser = new jsonparser(); public static final string tag_id = "id"; public static final string tag_cat = "category"; public static final string tag_title = "title"; public static final string tag_author = "author"; public static final string tag_date = "date"; public static final string tag_con = "content"; public static final string tag_img = "featured_image"; public static final string tag_date_cre = "date_created"; private static final string url_kbj = "https://api.url_api_here"; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.single_list_item); intent = getintent(); idkbj = i.getstringextra(tag_id); toast.maketext(getapplicationcontext(), "id berita = " + idkbj, toast.length_short).show(); new getdetail().execute(); } class getdetail extends asynctask<string,string, string>{ @override protected void onpreexecute(){ super.onpreexecute(); progressdialog = new progressdialog(detail.this); progressdialog.setmessage("loading..."); progressdialog.setindeterminate(false); progressdialog.setcancelable(true); progressdialog.show(); } protected string doinbackground(string... params){ try{ list<namevaluepair> params1 = new arraylist<>(); params1.add(new basicnamevaluepair("id", idkbj)); final jsonobject json = jsonparser.makehttprequest(url_kbj + "/" + idkbj + "/", "get", params1); //log.i("jsonda =", string.valueof(json)); final jsonarray data = json.getjsonarray("data"); final jsonobject object = data.getjsonobject(0); //log.e("json2 =", string.valueof(data)); runonuithread(new runnable() { @override public void run() { imageview thumb_img = (imageview) findviewbyid(r.id.featured_image); textview title = (textview) findviewbyid(r.id.title); //textview id = (textview) findviewbyid(r.id.id); textview category = (textview) findviewbyid(r.id.category); textview author = (textview) findviewbyid(r.id.author); textview date = (textview) findviewbyid(r.id.date); textview content = (textview) findviewbyid(r.id.content); try{ string cat_d = object.getstring(tag_cat); string tit_d = object.getstring(tag_title); string aut_d = object.getstring(tag_author); string con_d = object.getstring(tag_con); string url_detail_img = object.getstring(tag_img); jsonobject date_cre = object.getjsonobject(tag_date_cre); string date_d = date_cre.getstring(tag_date); category.settext(cat_d); title.settext(tit_d); author.settext(aut_d); content.settext(con_d); date.settext(date_d); picasso.with(getapplicationcontext()) .load(url_detail_img) .error(r.drawable.temp_img) .into(thumb_img); log.d("detail image = ", url_detail_img); } catch (jsonexception e){ e.printstacktrace(); } catch (exception e){ e.printstacktrace(); } } }); } catch (jsonexception e){ e.printstacktrace(); } return null; } protected void onpostexecute(string file_url){ progressdialog.dismiss(); } } } 
Comments
Post a Comment