Note that there are some explanatory texts on larger screens.

plurals
  1. POError with a JSON parser and a listview
    primarykey
    data
    text
    <p>I made an app for my website which takes som information from my website brainstorm.web44.net (those posts are just for testing!) and puts them in a custom listview.</p> <p>Here is the java code:</p> <pre><code>public class MainActivity extends ListActivity { JSONArray titles; HttpClient client; final static String URL = "http://brainstorm.net.com/?json=1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); client = new DefaultHttpClient(); String[] from = new String[] { "Title", "Description" }; int[] to = new int[] { R.id.txtTitle, R.id.txtContent }; List&lt;HashMap&lt;String, Object&gt;&gt; fillMaps; try { fillMaps = setContentAndTitle(new Read().execute("title").get(), new Read().execute("content").get()); SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_item, from, to); setListAdapter(adapter); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private List&lt;HashMap&lt;String, Object&gt;&gt; setContentAndTitle(String[] titles, String[] descriptions) { // TODO Auto-generated method stub List&lt;HashMap&lt;String, Object&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;(); for (int number = 0; number &lt; titles.length; number++) { HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;(); map.put("Title", titles[number]); map.put("Description", descriptions[number]); fillMaps.add(map); } return fillMaps; } public JSONArray Title() throws ClientProtocolException, IOException, JSONException { StringBuilder url = new StringBuilder(URL); HttpGet get = new HttpGet(url.toString()); HttpResponse r = client.execute(get); int status = r.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity e = r.getEntity(); String data = EntityUtils.toString(e); JSONArray brainstorm = new JSONArray(data); return brainstorm; } else { return null; } } public class Read extends AsyncTask&lt;String, Integer, String[]&gt; { @Override protected String[] doInBackground(String... arg0) { // TODO Auto-generated method stub List&lt;String&gt; titlesArray = new ArrayList&lt;String&gt;(); try { titles = Title(); for (int i = 0; i &lt; titles.length(); i++) { JSONObject title = titles.getJSONObject(i); String s = title.getString("title"); titlesArray.add(s); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[] arr = toStringArray(titlesArray.toArray()); return arr; } private String[] toStringArray(Object[] array) { // TODO Auto-generated method stub String[] arr = new String[array.length]; for(int i=0;i&lt;arr.length;i++){ arr[i]=array[i].toString(); } return null; } } } </code></pre> <p>Logcat :</p> <pre><code>06-27 18:11:32.768: E/AndroidRuntime(1809): Caused by: java.lang.NullPointerException 06-27 18:11:32.768: E/AndroidRuntime(1809): at com.tendariusprod.brainstorm.MainActivity.setContentAndTitle(MainActivity.java:64) 06-27 18:11:32.768: E/AndroidRuntime(1809): at com.tendariusprod.brainstorm.MainActivity.onCreate(MainActivity.java:43) </code></pre> <p>Json :</p> <pre><code>{ "status": "ok", "count": 2, "count_total": 2, "pages": 1, "posts": [ { "id": 17, "type": "post", "slug": "json-parser", "url": "http://brainstorm.web44.net/?p=17", "status": "publish", "title": "JSON Parser", "title_plain": "JSON Parser", "content": "&lt;p&gt;JSON Parser!&lt;/p&gt;\n", "excerpt": "&lt;p&gt;JSON Parser!&lt;/p&gt;\n", "date": "2013-06-27 09:02:55", "modified": "2013-06-27 09:02:55", "categories": [], "tags": [], "author": { "id": 1, "slug": "admin", "name": "admin", "first_name": "", "last_name": "", "nickname": "admin", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open", "custom_fields": { "single_layout": [ "0" ] } }, { "id": 14, "type": "post", "slug": "supermoon-ready-to-be-seen", "url": "http://brainstorm.web44.net/?p=14", "status": "publish", "title": "Supermoon ready to be seen!", "title_plain": "Supermoon ready to be seen!", "content": "&lt;p&gt;You will see the moon in its all splendore tonight!&lt;/p&gt;\n", "excerpt": "&lt;p&gt;You will see the moon in its all splendore tonight!&lt;/p&gt;\n", "date": "2013-06-26 16:56:19", "modified": "2013-06-26 17:18:11", "categories": [], "tags": [], "author": { "id": 1, "slug": "admin", "name": "admin", "first_name": "", "last_name": "", "nickname": "admin", "url": "", "description": "" }, "comments": [], "attachments": [], "comment_count": 0, "comment_status": "open", "custom_fields": { "single_layout": [ "0" ] } } ] } </code></pre> <p>Edit: this is a new code for the asynctask:</p> <pre><code> public class Read extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub try { StringBuilder url = new StringBuilder(URL); HttpGet get = new HttpGet(url.toString()); HttpResponse r = client.execute(get); int status = r.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity e = r.getEntity(); String data = EntityUtils.toString(e); JSONObject o =new JSONObject(data); titles = new JSONArray(o.getString("posts")); } else { Toast t = Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG); t.show(); } for (int i = 0; i &lt; titles.length(); i++) { JSONObject title = titles.getJSONObject(i); String s = title.getString("title"); String b = title.getString("content"); descArray.add(b); titlesArray.add(s); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); List&lt;HashMap&lt;String, Object&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;(); String[] arr1 = toStringArray(titlesArray.toArray()), arr2 = toStringArray(descArray .toArray()); for (int number = 0; number &lt; arr1.length; number++) { HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;(); map.put("Title", arr1[number]); map.put("Description", arr2[number]); fillMaps.add(map); String[] from = new String[] { "Title", "Description" }; int[] to = new int[] { R.id.txtTitle, R.id.txtContent }; SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, fillMaps, R.layout.list_item, from, to); setListAdapter(adapter); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload