Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

1.News.Java

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<String, Void, String> {
      @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<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
     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 < 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<String, String> createEmployee(String name, String number) {
      HashMap<String, String> employeeNameNo = new HashMap<String, String>();
      employeeNameNo.put(name, number);
      return employeeNameNo;
    }
    }
  1. getemp.php:

      <?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);
      ?>
    
  2. news.xml

       <?xml version="1.0" encoding="utf-8"?>
       <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" >
    
       <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" >
       </ListView>
    
      </RelativeLayout>
    
share|improve this question
    
possible duplicate of this and this –  turtle Nov 12 '14 at 7:19
    
Is your webservice part clear? Have you tested webservice/url in browser, getting proper results? –  VVB Nov 12 '14 at 7:22
    
Have you added internet permission <uses-permission android:name="android.permission.INTERNET"/> in manifest? –  VVB Nov 12 '14 at 7:23
    
Yes i have added the internet permission in the manifest and yes the webservice is running fine in the browser and the output is here as follows: {"emp_info":[{"employee name":"Adam","employee no":"101700"},{"employee name":"John","employee no":"101701"},{"employee name":"Paul","employee no":"101702"},{"employee name":"Mark","employee no":"101703"}]} This is the output I am getting after running in the browser –  eashdroidian Nov 12 '14 at 7:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.