title: Fetching data from Mysql DB to listView using JSON and PHP
body: <p>I am trying to fetch data from mysql DB in android listview. But somehow I am not been able to display the data in the listView </p>
  
  <p>I was trying this with the help of a tutorial,i.e., <a href="http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html" rel="noreferrer">http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html</a></p>
  
  <p><strong>Below is my MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
   private String jsonResult;
   private String url = "http://10.0.2.2/markit/login.php";
   private ListView listView;
   private TextView textv1;
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listView1);
    textv1=(TextView)findViewById(R.id.textView1);
    accessWebService();
   }
  
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
   }
  
   // Async Task to access the web
   private class JsonReadTask extends AsyncTask&lt;String, Void, String&gt; {
    @Override
    protected String doInBackground(String... params) {
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost(params[0]);
     try {
      HttpResponse response = httpclient.execute(httppost);
      jsonResult = inputStreamToString(
        response.getEntity().getContent()).toString();
     }
  
     catch (ClientProtocolException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }
     return null;
    }
  
    private StringBuilder inputStreamToString(InputStream is) {
     String rLine = "";
     StringBuilder answer = new StringBuilder();
     BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  
     try {
      while ((rLine = rd.readLine()) != null) {
       answer.append(rLine);
      }
     }
  
     catch (IOException e) {
      // e.printStackTrace();
      Toast.makeText(getApplicationContext(),
        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
     }
     return answer;
    }
  
    @Override
    protected void onPostExecute(String result) {
     ListDrwaer();
    }
   }// end async task
  
   public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[] { url });
   }
  
   // build hash set for list view
   public void ListDrwaer() {
    List&lt;Map&lt;String, String&gt;&gt; employeeList = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
  
    try {
     JSONObject jsonResponse = new JSONObject(jsonResult);
     JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
     for (int i = 0; i &lt; jsonMainNode.length(); i++) {
      JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
      String name = jsonChildNode.optString("employee_name");
      String number = jsonChildNode.optString("employee_no");
      String outPut = name + "-" + number;
      //textv1.setText(name);
      //textv1.setText(jsonResult);
      employeeList.add(createEmployee("employees", outPut));
     }
    } catch (JSONException e) {
     Toast.makeText(getApplicationContext(), "Error" + e.toString(),
       Toast.LENGTH_SHORT).show();
    }
  
    SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
      android.R.layout.simple_list_item_1,
      new String[] { "employees" }, new int[] { android.R.id.text1 });
    listView.setAdapter(simpleAdapter);
   }
  
   private HashMap&lt;String, String&gt; createEmployee(String name, String number) {
    HashMap&lt;String, String&gt; employeeNameNo = new HashMap&lt;String, String&gt;();
    employeeNameNo.put(name, number);
    return employeeNameNo;
   }
  }
  </code></pre>
  
  <p><strong>Below is mainactivity.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="TextView" /&gt;
  
      &lt;/TableRow&gt;
  
      &lt;ListView
          android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
           &gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Login.php</strong></p>
  
  <pre><code>&lt;?php
  $host="localhost"; //replace with database hostname 
  $username="root"; //replace with database username 
  $password=""; //replace with database password 
  $db_name="markit"; //replace with database name
  
  $con=mysql_connect("localhost", "root", "")or die("cannot connect"); 
  mysql_select_db("markit")or die("cannot select DB");
  $sql = "select * from emp_info"; 
  $result = mysql_query($sql);
  $json = array();
  
  if(mysql_num_rows($result)){
  while($row=mysql_fetch_assoc($result)){
  $json['emp_info'][]=$row;
  }
  }
  mysql_close($con);
  echo json_encode($json); 
  ?&gt; 
  </code></pre>