title: How to display data from a remote database into listview using php and mysql in android?
body: <p>Hello I am new to android and currently I am developing an Android app which connects to a remote database, fetches the data and display it in a listview. The problem is that when running the app it displays just a plain  listview but it does not show any data. Please help me out. I dont know where the problem really is. Even the logcat is not showing any errors. I am using xampp to run my machine as a local server and for the ip I have given my ipaddress. Thanks in advance
  This is my Java Class</p>
  
  <p>1.News.Java</p>
  
  <pre><code>package com.augustasoftsol.gmoapp;
  //importing all the necessary packages...
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.webkit.WebView.FindListener;
  import android.widget.Button;
  import android.widget.ImageButton;
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.support.v4.app.Fragment;  
  import android.view.LayoutInflater;  
  import android.view.View;  
  import android.view.ViewGroup; 
  import android.os.StrictMode;
  
  
  
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  
  
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.TextView;
  import android.widget.Toast;
  public class News extends Fragment
  {  
  private String jsonResult;
   private final String url = "http://myipaddress/GMO/getemp.php";
   private ListView listView;
  
  
  @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container,  
             Bundle savedInstanceState) 
  {  
  
       View rootView = inflater.inflate(R.layout.news,container,false);
  
  
       return rootView;  
   }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) 
  {
      // TODO Auto-generated method stub
  
      super.onActivityCreated(savedInstanceState);
  
       listView = (ListView)getView().findViewById(R.id.listView1);
       accessWebService();
  
  }
  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(getActivity(),
            "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();
          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;();
       String[] emp = {"employee"};
       int[] id = {android.R.id.text1};
        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.getString("employee name");
          String number = jsonChildNode.getString("employee no");
          String outPut = name + "-" + number;
          employeeList.add(createEmployee("employees", outPut));
         }
        } catch (JSONException e) {
         Toast.makeText(getActivity(), "Error" + e.toString(),
           Toast.LENGTH_SHORT).show();
        }
  
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList, android.R.layout.simple_list_item_1,emp ,id);
        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>
  
  <ol start="2">
  <li><p>getemp.php:</p>
  
  <pre><code>  &lt;?php
    $host="localhost"; //replace with database hostname
    $username="myusername"; //replace with database username
    $password="mypwd"; //replace with database password
    $db_name="gmo"; //replace with database name
  
    $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")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></li>
  <li><p>news.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"
     android:background="#ffffff"
     android:orientation="vertical" &gt;
  
     &lt;ListView
     android:id="@+id/listView1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/searchit"
     android:layout_marginTop="24dp" &gt;
     &lt;/ListView&gt;
  
    &lt;/RelativeLayout&gt;
  </code></pre></li>
  </ol>