我想使用PHP从MySQL数据库中填充Android XML中的表格布局。
我用PHP查询了数据库,我能够将Strings从PHP文件传递给我的Java。如何传递整个结果数组以填充我的表?
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);
}
}
在这里输入代码
$ 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;
}
答案 0 :(得分:1)
您还没有共享您的PHP代码,但它的工作方式,您将PHP数组编码为JSON数据,然后您的应用程序将能够将json字符串解析为java对象。
一些有用的指示:
了解JSON:
一些JSON库:
PHP
PHP代码
$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);