Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
 
 
public class First_Activity extends ListActivity {
 
    String myJSON;
 
    private static final String TAG_RESULTS="result";
    private static final String TAG_ID = "Sec_Cust_ID";
    private static final String TAG_NAME = "Sec_Cust_Name";
    private static final String TAG_ADD ="Sec_Account_State";
 
    JSONArray accounts = null;
 
    ArrayList<hashmap><string,>> accountlist;
 
    ListView list;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_);
        list = (ListView) findViewById(R.id.baccountlist);
        accountlist = new ArrayList<hashmap><string,string>>();
        getData();
    }
 
 
    protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            accounts = jsonObj.getJSONArray(TAG_RESULTS);
 
            for(int i=0;i<accounts.length();i++){>
                JSONObject c = accounts.getJSONObject(i);
                String Sec_Cust_ID = c.getString(TAG_ID);
                String Sec_Cust_Name = c.getString(TAG_NAME);
                String Sec_Account_State = c.getString(TAG_ADD);
 
                HashMap<string,string> persons = new HashMap<string,string>();
 
                persons.put(TAG_ID,Sec_Cust_ID);
                persons.put(TAG_NAME,Sec_Cust_Name);
                persons.put(TAG_ADD,Sec_Account_State);
 
                accountlist.add(persons);
            }
 
            ListAdapter adapter = new SimpleAdapter(
                    First_Activity.this, accountlist, R.layout.list_item,
                    new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                    new int[]{R.id.ac_id, R.id.ac_name, R.id.ac_state}
            );
 
            list.setAdapter(adapter);
 
        } catch (JSONException e) {
            e.printStackTrace();
        }
 
    }
 
    public void getData(){
        class GetDataJSON extends AsyncTask<string,>{
 
            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://10.0.2.2:80/AEST_SYSTEM/get-data.php");
 
                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");
 
                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
 
                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
 
                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }
 
            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }
 
   
 }
Posted
Updated 23-Aug-15 1:20am
v2
Comments
Richard MacCutchan 23-Aug-15 7:18am    
Please do not just dump your code and expect someone to test it and fix any problems for you. Remove all the code not relevant to your problem, and explain exactly what happens (or does not), and where the error occurs. Please also use <pre> tags around the code so it is properly formatted and readable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900