我目前正在努力如何正确地显示链接到PHP中另一个表的表中的详细信息。现在我的Android代码有多个显示不同细节的片段,我可以将这些信息从php显示到android代码中,但是当我登录用户名时,这些细节的显示方式不同。
让我说我有个人资料片段,余额片段和记录片段。现在,当我登录到给定的用户名时,这些片段会显示其他用户详细信息。
此外,在我的PHP服务器中,我创建了3个表,每个表都相互链接。在我的PHP代码中,我为每个表创建单独的文件,因为我遵循互联网上可以从php获取数据的一些指南。
表1:
表2
这是balance.php代码:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from account_details";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"Balance"=>$row['Balance'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
这是parking_record.php代码
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from parking_record";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"Parking_Amount"=>$row['Parking_Amount'],
"Date_Time"=>$row['Date_Time'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
最后这是profile_details.php代码
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from profile_details";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"FullName"=>$row['FullName'],
"DateOfBirth"=>$row['DateOfBirth'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
Balance.java
public class dri_balance extends ArrayAdapter<String> {
private String[] Acc_Balance;
private Activity context;
public dri_balance(Activity context, String[] Acc_Balance) {
super(context, R.layout.fragment_balance, Acc_Balance);
this.context = context;
this.Acc_Balance = Acc_Balance;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.fragment_balance, null,
true);
TextView tvBlnf = (TextView) listViewItem.findViewById(R.id.tvBlnf);
tvBlnf.setText(Acc_Balance[position]);
return listViewItem;
}
}
BalanceFragment.java
public class BalanceFragment extends Fragment implements
View.OnClickListener {
public BalanceFragment() {
// Required empty public constructor
}
public static final String JSON_URL =
"http://192.168.1.2/json_balance_records.php";
private Button buttonGet;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_listview, container,
false);
buttonGet = (Button) rootView.findViewById(R.id.buttonGet);
buttonGet.setOnClickListener(this);
listView = (ListView) rootView.findViewById(R.id.listView);
return rootView;
//return inflater.inflate(R.layout.fragment_profile, container, false);
}
private void sendRequest(){
StringRequest stringRequest = new StringRequest(JSON_URL, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(),error.getMessage(),
Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue =
Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(stringRequest);
}
private void showJSON(String json){
Dri_Balance pj = new Dri_Balance(json);
pj.parseJSON();
dri_balance cl = new dri_balance(getActivity(),
Dri_Balance.Acc_Balance);
listView.setAdapter(cl);
}
@Override
public void onClick(View v) {
sendRequest();
}
}
parseJson Balance.java
public class Dri_Balance {
public static String[] Acc_Balance;
public static final String JSON_ARRAY = "result";
public static final String KEY_DriBalance = "Acc_Balance";
private JSONArray users = null;
private String json;
public Dri_Balance(String json){
this.json = json;
}
public void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
Acc_Balance = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
Acc_Balance[i] = jo.getString(KEY_DriBalance);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
如何在Android中显示正确的数据?
我很抱歉这个问题太多,我确实在搜索互联网,但它没有给我我想要的东西。我知道我要求别人为我做作业,但我对Android和PHP都不熟悉。
我已经编辑了balance.php代码(版本2):
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select Balance from account_details WHERE Username='$Username'";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"Balance"=>$row['Balance'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
我尝试运行php文件,它给了我null值
{
"result": [{
"Balance": null
}]
}
已编辑的balance.php代码(第3版)
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$Username = $_GET["Username"];
$mysqli = mysqli_connect(HOST,USER,PASS,DB);
$stmt = $mysqli->prepare ("SELECT Balance from account_details WHERE
Username=?");
$stmt->bind_param('s', $Username);
$stmt->execute();
$stmt->bind_result($Balance);
$rows = array();
while($stmt->fetch()) {
array_push($rows, array('Balance'=>$Balance));
}
$stmt->close();
$mysqli->close();
echo json_encode($rows);
?>
结果
[{"Balance":"50.512"}]
好的,我已成功加入PHP表格,可以得到我想要的结果。现在我尝试运行我的Android代码,当我点击显示数据时崩溃,如何更正代码?
答案 0 :(得分:0)
为了完整性,并总结评论对话,以下是所需的修复。原始基于PHP的JSON端点中的SQL查询没有WHERE
子句,因此他们会将整个表传送到移动应用程序,而不是所需的记录。
此外,每个端点请求都不需要安全性详细信息,因此只要拥有用户名,互联网上的任何人都可以检索任何数据。我们在此处添加了密码,并建议将该应用程序包含凭据对话框和相应的本地存储作为未来工作的项目,以便用户可以识别自己。
有一个SQL注入漏洞,在MySQLi驱动程序中使用参数绑定修复了该漏洞。
已经注意到密码不应该以明文形式存储。这里需要一个兼容Java和PHP的散列系统。
您可能想要考虑如何实现这些: