Reputation: 93
I want to populate my table layout in Android XML from a MySQL database using PHP.
I have queried the database with the PHP and I am able to pass Strings from the PHP file to my Java. How do I pass the whole result array to populate my table?
public TransactionLogConnection(String vuname, String vpword)
{
vendorusername = vuname;
vendorpassword = vpword;
}
public String dbresult()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://cediline.com/TransactionLog.php");
String text;
try {
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("vendor_username", vendorusername));
nameValuePairs.add(new BasicNameValuePair("vendor_password", vendorpassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
text = new String(baf.toByteArray());
return text;
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
private class Load_Log extends AsyncTask<String, Void, String>{
String username;
String password;
public Load_Log(String uname, String pword)
{
username = uname;
password = pword;
}
@Override
protected String doInBackground(String... arg0) {
TransactionLogConnection con = new TransactionLogConnection(username,password);
return con.dbresult().replace("\n", "");
}
@Override
protected void onPreExecute() {
shout("Updating...");
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String result) {
for(int count = 0; count<=2; ++count)
{
newtext.setText(result);
newRow.addView(newtext);
}
newtext.setText(result);
newRow.addView(newtext);
tbl.addView(newRow);
pb.setVisibility(View.INVISIBLE);
}
}
enter code here
$vendor_username = $_POST["vendor_username"];
$vendor_password = $_POST["vendor_password"];
$sql="select phonenumber from confidential_detail where username = '$vendor_username' and password = '$vendor_password'";
$result = mysql_query($sql);
if (!$result)
{
echo "error:unable to load log";
}
else
{
$phonenumber = mysql_result($result, 0);
$sql="select amount, date, creditor_phone_number from voucher_records where debitor_phone_number = '$phonenumber'";
$result = mysql_query($sql);
if($result)
{
return $result;
}
else
{
echo "error:unable to load log";
}
//return $balance;
}
Upvotes: 0
Views: 195
Reputation: 45500
You have not shared your php code, but the way it works , you will encode PHP array into JSON data, then your app will be able to parse the json string into java objects.
Some helpful pointers:
Read up about JSON:
Some JSON libraries:
PHP
PHP CODE
$result = mysql_query($sql);
if (!$result) {
echo "Unable to get log: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$row = mysql_fetch_assoc($result);
echo json_encode($row);
Upvotes: 1