Reputation: 41
I have a JSON array that I am trying to display. I was able to succesfully display it as a textview, however not all of the data is listed. only the last. Which is "Remember tomorrow we will be giving out popcorn and some more water bottles at our office! Tuesday, we will give you free breakfast! Please sign up to be a CSA volunteer when coming to get some free food!". I believe that it would be more efficient to list it as a listview however, I everything that I am reading from online websites isn't working.
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
try {
JSONObject jObject = new JSONObject(result);
JSONArray jsonArray = jObject.getJSONArray("notices");
for(int i = 0; i < jsonArray.length(); i++) {
String arrayString = jsonArray.getString(i);
Log.d("notices", arrayString);
ListView listView1 = (ListView) findViewById(R.id.listView1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Basicly I want to display this as a listview !
Upvotes: 0
Views: 2603
Reputation: 9373
First you should think about doing this in an async task.
Second to display it in a lsitview is relatively easy. You can do something like this:
public class YourActivity extends Activiy{
public void getJson(String selection, String url) {
new LoadJsonTask().execute( selection, url);
}
private class LoadJsonTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {
ProgressDialog dialog ;
protected void onPreExecute (){
dialog = ProgressDialog.show(YourActivity.this ,"title","message");
}
protected ArrayList<HashMap<String, String>> doInBackground (String... params){
return doGetJson(params[0],params[1]);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> mylist){
ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,
new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
R.id.item_subtitle, R.id.timestamp});
setListAdapter(adapter);
dialog.dismiss();
}
}
public ArrayList<HashMap<String, String>> doGetJson(String selection, String url) {
JSONObject json = null;
String formatedcat = selection.toLowerCase();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
json = JSONfunctions
.getJSONfromURL(url);
try {
//the array title that you parse
JSONArray category = json.getJSONArray(formatedcat);
for (int i = 0; i < category.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = category.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name",
c.getString("title"));
//map.put("text",c.getString("title"));
map.put("ts",c.getString("run_date") );
map.put("image","http:"+c.getString("url"));
mylist.add(map);
}
} catch (JSONException e) {
}
return mylist;
....
}
The important part of the code is:
ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,
new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
R.id.item_subtitle, R.id.timestamp});
setListAdapter(adapter);
This is where you are setting the string array to the hashmap you created from the json object and setting the list adapter with it so the listview will display all the relevant data. See more with this question.
Upvotes: 1