- Forum posts: 1
Jul 23, 2013, 10:59:46 AM via Website
Jul 23, 2013 10:59:46 AM via Website
I created a sample android application get the data from the php page connected to mysql db in my localhost.
the data passed correctly and I see all the record in the app.
But I want to put this data in listview, because I want to show only the name when the user press in the name on listview open the details info about.
So please can anyone help me how I can put the record in ListView.
Database:
2 `FirstName` varchar(20) DEFAULT NULL,
3 `LastName` varchar(20) DEFAULT NULL,
4 `Age` varchar(20) DEFAULT NULL,
5 `Mobile` varchar(20) DEFAULT NULL
6) ENGINE=InnoDB DEFAULT CHARSET=latin1;
PHP page:
2 $con=mysql_connect("localhost","root","");
3
4 if(!$con)
5 die('could not connect: ' .mysql_error());
6
7 mysql_select_db("mydatabase",$con);
8
9 $result = mysql_query("SELECT * FROM CUSTOMER");
10
11 while($row=mysql_fetch_assoc($result)){
12 $output[]=$row;
13 }
14
15 print(json_encode($output));
16
17 mysql_close($con); ?>
The Java Android Code:
2
3import java.io.BufferedReader;
4import java.io.InputStream;
5import java.io.InputStreamReader;
6
7import org.apache.http.HttpEntity;
8import org.apache.http.HttpResponse;
9import org.apache.http.client.HttpClient;
10import org.apache.http.client.methods.HttpPost;
11import org.apache.http.impl.client.DefaultHttpClient;
12import org.json.JSONArray;
13import org.json.JSONObject;
14
15import android.os.Bundle;
16import android.os.StrictMode;
17import android.app.Activity;
18import android.util.Log;
19import android.view.Menu;
20import android.widget.TextView;
21
22public class MainActivity extends Activity {
23
24 TextView nameView;
25 TextView ageView;
26 TextView jobView;
27 @Override
28 public void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.activity_main);
31 StrictMode.enableDefaults(); //STRICT MODE ENABLED
32
33 nameView = (TextView) findViewById(R.id.nametxt);
34 ageView = (TextView) findViewById(R.id.agetxt);
35 jobView = (TextView) findViewById(R.id.jobtxt);
36
37 getData();
38 }
39
40 public void getData(){
41 String result = "";
42 InputStream isr = null;
43 try{
44 HttpClient httpclient = new DefaultHttpClient();
45 HttpPost httppost = new HttpPost(""); //YOUR PHP SCRIPT ADDRESS
46 HttpResponse response = httpclient.execute(httppost);
47 HttpEntity entity = response.getEntity();
48 isr = entity.getContent();
49 }
50 catch(Exception e){
51 Log.e("log_tag", "Error in http connection "+e.toString());
52 nameView.setText("Couldnt connect to database");
53 }
54 //convert response to string
55 try{
56 BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
57 StringBuilder sb = new StringBuilder();
58 String line = null;
59 while ((line = reader.readLine()) != null) {
60 sb.append(line + "\n");
61 }
62 isr.close();
63
64 result=sb.toString();
65 }
66 catch(Exception e){
67 Log.e("log_tag", "Error converting result "+e.toString());
68 }
69
70 //parse json data
71 try {
72 String n = "";
73 String a="";
74 String j="";
75 JSONArray jArray = new JSONArray(result);
76
77 for(int i=0; i<jArray.length();i++){
78 JSONObject json = jArray.getJSONObject(i);
79 n = n + "Name : "+json.getString("FirstName")+" "+json.getString("LastName")+"\n";
80 a= a + "Age : "+json.getInt("Age")+"\n";
81 j= j + "Job : "+json.getString("Job")+"\n";
82 }
83
84 nameView.setText(n);
85 ageView.setText(a);
86 jobView.setText(j);
87
88 } catch (Exception e) {
89
90 Log.e("log_tag", "Error Parsing Data "+e.toString());
91 }
92
93 }
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.