Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can parse with a single asynctask and display data in listview. I have used custom listview to display the content and title.</p> <pre><code> public class MainActivity extends ListActivity { JSONArray titles; HttpClient client; ProgressDialog pd; final static String URL = "http://brainstorm.web44.net/?json=1"; ArrayList&lt;String&gt; title = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; content = new ArrayList&lt;String&gt;(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pd = new ProgressDialog(MainActivity.this); pd.setMessage("Loading..."); client = new DefaultHttpClient(); new Read().execute(); } public class Read extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected void onPreExecute() { // TODO Auto-generated method stub pd.show(); super.onPreExecute(); } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); pd.dismiss(); CustomAdapter cus = new CustomAdapter(); setListAdapter(cus); } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); 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(); String result=sb.toString(); try { JSONObject jsono = new JSONObject(result); JSONArray jsonarray = new JSONArray(jsono.getString("posts")); for(int i=0;i&lt;jsonarray.length();i++) { JSONObject job1 = (JSONObject) jsonarray.get(i); String titl = job1.getString("title"); String con = job1.getString("content"); title.add(titl); content.add(con) ; Log.i("......", titl); Log.i("......", con); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } class CustomAdapter extends BaseAdapter { LayoutInflater mInflater; public CustomAdapter() { mInflater = LayoutInflater.from(MainActivity.this); } @Override public int getCount() { // TODO Auto-generated method stub return title.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item, parent, false); holder = new ViewHolder(); holder.tv1 = (TextView) convertView.findViewById(R.id.textView1); holder.tv2 = (TextView) convertView.findViewById(R.id.textView2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv1.setText(title.get(position)); holder.tv2.setText(content.get(position)); return convertView; } } static class ViewHolder { TextView tv1,tv2; } } </code></pre> <p>list_itemt.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="TextView" /&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="22dp" android:text="TextView" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Snap shot</p> <p><img src="https://i.stack.imgur.com/jQa74.png" alt="enter image description here"></p>
 

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