I am totally new to java/android . I have searched many tutorials over the internet. But those didnt helped me. So I have posted this problem here.
I have simple json data in php like below,
[{"title":"re3","notice":"etrf4w"},{"title":"vfdxg","notice":"fdgd"}, {"title":"notice1","notice":"details..."}]
I want to show this json data in android as a plain text like below,
Title:re3
notice: etrf4w ..................... ...................
How could I do that. please help me.
Here is the exmaple
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 1:{
showList();
}break;
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
/*
Your initialization code here
*/
new Thread(){
@Override
public void run() {
String resp = loadURLData(JSON_URL);
MainActivity.this.items = new ArrayList<Item>();
try{
JSONArray items = new JSONArray(resp);
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
String title = item.getString("title");
String notice = item.getString("notice");
Item it = new Item(title, notice);
}
handler.sendEmptyMessage(1);
}catch(JSONException jSONEx){
}
}
}.start();
}
private void showList(){
ListAdapter adapter = new MyAdapter(this, items);
listView.setAdapter(adapter);
}
private static String loadURLData(String msgsUrl){
try{
URL url = new URL(msgsUrl);
URLConnection conn = url.openConnection();
InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(streamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line);sb.append("
");
}
br.close();
String resp = sb.toString();
return resp;
}catch(IOException iOEx){
return "";
}
}
Try this..
[ => JSONArray
{ => JSONObject
String response = "Your Response in String format";
JSONArray new_array = new JSONArray(response);
for(int i = 0; i < new_array.lenght; i++){
JSONObject obj = new_array.getJSONObject(i);
// You can get Title from obj.getString("title") like below
System.out.println("Title:"+obj.getString("title"));
// You can get Notice from obj.getString("notice") like below
System.out.println("Notice:"+obj.getString("notice"));
}
JSON Parsing turorials
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
[ // json array node
{ // json object node
"title": "re3",
"notice": "etrf4w"
},
{
"title": "vfdxg",
"notice": "fdgd"
},
{
"title": "notice1",
"notice": "details"
}
]
To parse
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
JSOnArray jr = new JSONArray("json string");
for(int i=0;i<jr.length();i++)
{
JSONObject jb = (JSONObject) jr.getJSONObject(i);
String title = jb.getString("title");
String notice =jb.getString("notice");
HashMap<String,String> map = new HashMap<String,String>();
map.add("key1",title);
map.add("key2",notice);
list.add(map);
}
Now you can use the list
to display the same in a listview.
Try this code
JSONArray jsonArray = new JSONArray(responseData);
ArrayList<String> list =new ArrayList<String>();
if(jsonArray != null){
for(int i=0 ;i < jsonArray.length() ;i++){
list.add(jsonArray.getJSONObject(i).getString("title").toString());
list.add(jsonArray.getJSONObject(i).getString("notice").toString());
}
}
return list