[Solved] Retrieve json Data and bind it to listview [duplicate]


This may help you..

Your Actmain class

Actmain.java

public class Actmain extends Activity {

// url to make request
private static String url = "http://api.androidhive.info/contacts/";

// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
private ArrayList<HashMap<String, String>> _alistHashmap; 
private Clsgetjson getjson;

// contacts JSONArray
private JSONArray Jarray = null;
private JSONObject jobj;
private Listview lv;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

// Creating JSON Parser instance
getjson = new Clsgetjson();

 _alistHashmap = new ArrayList<HashMap<String, String>>();

lv=(listview)findviewbyid(R.id.lv);


 jobj= getjson.getJSONFromUrl(url);



  try {
        // Getting Array of Contacts
        Jarray = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for(int i = 0; i < contacts.length(); i++){
            JSONObject jobject = Jarray.getJSONObject(i);

            // Storing each json item in variable
            String id = jobject.getString(TAG_ID);
            String name = jobject.getString(TAG_NAME);
            String email = jobject.getString(TAG_EMAIL);
            String address = jobject.getString(TAG_ADDRESS);
            String gender = jobject.getString(TAG_GENDER);

            // Phone number is agin JSON Object
            JSONObject phone = jobject.getJSONObject(TAG_PHONE);
            String mobile = phone.getString(TAG_PHONE_MOBILE);
            String home = phone.getString(TAG_PHONE_HOME);
            String office = phone.getString(TAG_PHONE_OFFICE);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            map.put(TAG_EMAIL, email);
            map.put(TAG_PHONE_MOBILE, mobile);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }



    //now set your adapter to listview (you can do this under buttons onclick event)


String[] from=new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE };
int[] to= new int[] {R.id.name, R.id.email, R.id.mobile }

SimpleAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.raw_lv,from,to);

lv.setAdapter(adapter);

now Clsgetjson.java

public class Clsgetjson {


static JSONObject jObj = null;
static String strjson = "";


public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

strjson=EntityUtility.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }



    // try parse the string to a JSON object
    try {


        jObj = new JSONObject(strjson);


    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
}

or with parameter

        List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
    nvp.add(new BasicNameValuePair("function", "login"));
    nvp.add(new BasicNameValuePair("uname", username));
    nvp.add(new BasicNameValuePair("pwd", pass)));

add this in request

        if(nvp!=null)

        hpost.setEntity(new UrlEncodedFormEntity(nvp));

everything else is same as above

solved Retrieve json Data and bind it to listview [duplicate]