CODES FOR Android:
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 org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import example.david.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Viewall extends Activity {
String JsonResult;
ListView vx;
JSONArray Rdetails;
ArrayList<HashMap<String, String>>registerList;
private String URLRetrievedetails ="http://10.0.2.2/nanyonga/retrive.php";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewall);
vx=(ListView)findViewById(R.id.va);
registerList=new ArrayList<HashMap<String, String>>();
getData();
}
private class JsonReaddetails 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(getApplicationContext(),"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ShowResult();
}
}
// end async task
public void getData() {
JsonReaddetails pdetails = new JsonReaddetails();
// passes values for the urls string array
pdetails.execute(new String[] { URLRetrievedetails });
}
// build hash set for list view
public void ShowResult() {
// either you can call ArrayList here or in the method
//personalList = new ArrayList<HashMap<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(JsonResult);
Rdetails = jsonResponse.getJSONArray("Register");
// here we are looping
for (int i = 0; i < Rdetails.length(); i++) {
JSONObject y = Rdetails.getJSONObject(i);
String first= y.getString("FirstName");
String last = y.getString("LastName");
String tel = y.getString("Telephone");
String uname = y.getString("Username");
String pass = y.getString("Password");
String outPut ="\n\n" +"FName=:"+" "+first+"\n"+"LName=:"+" "+last+"\n"+"Tel=:"+" "+tel+"\n"+"UserN=:"+" "+uname+"\n"+"PasswOrd=:"+pass+"\n";
registerList.add(CreateRegister("registerdetails", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, registerList,android.R.layout.simple_list_item_1,
new String[] { "registerdetails" }, new int[] { android.R.id.text1 });
vx.setAdapter(simpleAdapter);
}
// this definies the table.You can use all field or you can use afew
private HashMap<String, String> CreateRegister(String firsname, String lastname) {
HashMap<String, String> registerList = new HashMap<String, String>();
registerList.put(firsname, lastname);
return registerList;
}
//===============================================================================================
// the view /display codes endes here
//==========================================================================================

