java - How to parse JSON and display into activity? -
goal
my welcomescreen
activity that's welcome screen when user click button fetch google news
, return json displaymessageactivity here want display json beautifully news feed style...
achieved
so far achieved json , using intent move response newsfeed activity. do.?
tried:
i followed hooking custom layout listview , i'm here.. next session different didn't followed..
displaymessageactivity.java
public class displaymessageactivity extends actionbaractivity { private listview newslistview; //private string[] stringarray; private arrayadapter newsitemarrayadapter; private static final string debug_tag = "http"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_display_message); //newsitemarrayadapter = new arrayadapter(displaymessageactivity.this, android.r.layout.simple_list_item_1, stringarray); newsitemarrayadapter = new newsadapter(displaymessageactivity.this, new string[10]); newslistview = (listview) findviewbyid(r.id.listviewid); newslistview.setadapter(newsitemarrayadapter); // message intent //intent intent = getintent(); //string message = intent.getstringextra(welcomescreenactivity.extra_message); //log.d(debug_tag, "the response is: " + message); //setcontentview(r.layout.activity_display_message); }
newsadapter.java
public class newsadapter extends arrayadapter{ private layoutinflater inflater; public newsadapter(activity activity, string[] items){ super(activity, r.layout.news_feed, items); inflater = activity.getwindow().getlayoutinflater(); } @override public view getview(int position, view convertview, viewgroup parent){ return inflater.inflate(r.layout.news_feed, parent, false); } }
news_feed.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"> <textview android:id="@+id/title" android:text="@string/news_title" android:layout_width="match_parent" android:layout_height="match_parent" android:textsize="19sp" /> </linearlayout>
update:
after searching found how parse json in android answer ...
tried , throws ..
org.json.jsonexception: unterminated string @ character 500 of {"responsedata": {"results":[{"g
any idea ?
there hundreds of tutorials this, should try gson. have create class attributes want parse json. class :
public class data { private string name; private int age; public string getname() { return this.name; } public int getage() { return this.age; } public void setname(string name) { this.name = name; } public void setage(int age) { this.age = age; } }
and json :
{ "name": "whatever", "age": 21 }
you can :
public void setdata(string jsonasstring) { final gson gson = new gsonbuilder().create(); final data data = gson.fromjson(jsonasstring, data.class); //and of course access data string name = data.getname(); }
of course don't have have json's variables in class. in case variable name
would've work.
but of course, best learn how find answers yourself...
Comments
Post a Comment