[{"linkName":"Microsoft","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/cfbab4b1-92fa-4401-ae4c-f921259ea5b9.jpg","linkURL":"http://www.microsoft.com ","linkID":"10","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"-1"},{"linkName":"RBA","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/919635d7-4bfa-4ee3-b9c9-3091153f12ea.jpg","linkURL":"http://www.rbaconsulting.com","linkID":"11","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"-1"},{"linkName":"ViaWest","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/7e266ef5-3f71-46ab-8a0a-86ca9516178a.png","linkURL":"http://www.viawest.com","linkID":"12","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"9"},{"linkName":"RBA","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/60c809d6-7937-41b3-934b-fb4d249a7f20.png","linkURL":"http://www.rbaconsulting.com","linkID":"14","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"11"},{"linkName":"Microsoft","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/7e12c518-7336-4829-88da-526722960e21.png","linkURL":"http://www.microsoft.com ","linkID":"13","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"10"}]
From above json data i have to bind link name and linkurl in listview....can anybody please help,the problem is i am a newbie and and i have gone through many tutorials but those were really complicated,most of them use the same example....earthquakes n all that! but those json data contains { "earthquakes":[{"eqid":"c0001xgp","magnitude":8.8,.... i,e curly braces before the square brackets but mine json data is different as u can see! i have sucessfully run these examples but when i use my link the app crashes, so please share your knowledge to help me ! thanks
this is the code i am using....
import...
public class Main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<hashmap><string,>> mylist = new ArrayList<hashmap><string,>>();
Log.i("development",mylist.toString());
JSONObject json = JSONfunctions.getJSONfromURL("http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo");
try{
JSONArray earthquakes = json.getJSONArray("earthquakes");
for(int i=0;i<earthquakes.length();i++){>
HashMap<string,> map = new HashMap<string,>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("eqid"));
map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "magnitude" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView @SuppressWarnings("unchecked")
HashMap<string,> o = (HashMap<string,>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
and
import....
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}