这是MySQL数据(表工作细节)
通过以下代码检索的数据。注意到我想要检索除 id 和 twd 之外的所有内容。
private void showForce(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(Configs.TAG_JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
String project = c.getString(Configs.TAG_PROJECT).trim();
RetrieveProject(project);
String description = c.getString(Configs.TAG_WORKDESCRIPTION).trim();
int per = c.getInt(Configs.TAG_PERCENTAGE);
String in=c.getString(Configs.TAG_IN).trim();
String time[]=in.split(":", 3);
String time1=time[0]+":"+time[1];
String out=c.getString(Configs.TAG_OUT).trim();
String timeOut[]=out.split(":", 3);
String timeout=timeOut[0]+":"+timeOut[1];
seekBar.setProgress(per);
progressText.setText("Covered:" + "" + seekBar.getProgress() + "/" + seekBar.getMax());
String From=c.getString(Configs.TAG_From).trim();
String To=c.getString(Configs.TAG_To).trim();
String Mil=c.getString(Configs.TAG_Mileage).trim();
String Hotel=c.getString(Configs.TAG_Hotel).trim();
String Toll=c.getString(Configs.TAG_Toll).trim();
String Business=c.getString(Configs.TAG_Business).trim();
TimeIn.setText(time1);
TimeOut.setText(timeout);
Description.setText(description);
mileage.setText(Mil);
hotel.setText(Hotel);
business.setText(Business);
toll.setText(Toll);
travelFrom.setText(From);
travelTo.setText(To);
} catch (JSONException e) {
e.printStackTrace();
}
比索
<?php
define('HOST','localhost');
define('USER','spiral69_wp178');
define('PASS','q1w2e3r4');
define('DB','spiral69_androiddb');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');
$id=$_GET['id'];
$sql = "select * from work_details WHERE id= '". $id."'";
$res = mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'project'=>$row[1],'work_description'=>$row[2],'percentage'=>$row[3],'timeIn'=>$row[4],
'timeOut'=>$row[5],'travel_From'=>$row[6],'travel_To'=>$row[7],'mileage'=>$row[8],'hotel_accom'=>$row[9],'toll'=>$row[10],'business_expenses'=>$row[11]));
}
echo (json_encode(array("result"=>$result)));
mysqli_close($con);
?>
项目,说明,TimeIn,TimeOut,每个显示正确,但From,To,Mil,Hotel,Toll,Business不正确。
这是我在setText
中得到的输出。(不正确)
travelFrom.setText(From); I get 63 (twd column)
travelTo.setText(To); I get df (travel_from column)
mileage.setText(Mil); I get a....
答案 0 :(得分:2)
当你在PHP脚本中设置json对象中的数据时,你错过了'twd'列...'travel_from'的索引是7而不是6,同样适用于之后的其他字段!! !