这是work_details 表。现在我想从MySQL
检索timeIn和timeOut数据到android。
public void RetrieveTotalHours( final String ID)
{
class GetHours extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(),"Fetching...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
showHours(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequestParam(Configs.RETRIEVE_HOURS,ID);
return s;
}
}
GetHours ge = new GetHours();
ge.execute();
}
private void showHours(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(Configs.TAG_JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
//iD = c.getString(Configs.TAG_ID);
String MiNtimeIn = c.getString(Configs.TAG_IN);
String MaXtimeOut=c.getString(Configs.TAG_OUT);
Log.e("A",MiNtimeIn);
Log.e("S", MaXtimeOut);
//total.setText(MiNtimeIn);
} catch (JSONException e) {
e.printStackTrace();
}
}
retrieveTotalHours.php
<?php
define('HOST','127.0.0.1:3307');
define('USER','root');
define('PASS','');
define('DB','androiddb');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');
$twd= $_GET['id'];
$sql = "select timeIn, timeOut from work_details WHERE twd = '".$twd."' AND id IN
(SELECT MIN(id) FROM work_details WHERE twd ='".$twd."' UNION SELECT MAX(id) FROM work_details WHERE twd='".$twd."')";
$res = mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
array_push($result,array('timeIn'=>$row[0],'timeOut'=>$row[1]));
}
echo json_encode($result);
mysqli_close($con);
?>
假设 ID (twd) 为 8,因此 String MiNtimeIn
假设显示 21:52(id 3),String MaXtimeOut
显示 1:52 (id 4),但是我明白了
01-10 23:40:09.350 28936-28936/com.example.project.myapplication D/TextLayoutCache﹕ Enable myanmar Zawgyi converter
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ org.json.JSONException: Value [{"timeOut":"23:52:00","timeIn":"21:52:00"},{"timeOut":"01:52:00","timeIn":"21:52:00"}] of type org.json.JSONArray cannot be converted to JSONObject
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:159)
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:172)
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails.showHours(Edit_WorkDetails.java:248)
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails.access$000(Edit_WorkDetails.java:46)
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails$1GetHours.onPostExecute(Edit_WorkDetails.java:232)
01-10 23:40:09.420 28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails$1GetHours.onPostExecute(Edit_WorkDetails.java:220)
How can I retrieve the timeIn from min id and timeOut from max id where twd = 8 ?
谢谢。
已编辑
private void showHours(String json) {
try {
JSONArray array;
for(int n = 0; n < array.length(); n++)
{
JSONObject c = array.getJSONObject(0);
String MiNtimeIn = c.getString(Configs.TAG_IN);
String MaXtimeOut=c.getString(Configs.TAG_OUT);
Log.e("A",MiNtimeIn);
Log.e("S", MaXtimeOut);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
已编辑
假设 twd 是 10,我应该得到 1:50:00 和 6:51:00,但我得到了
01-11 02:00:09.900 2872-2872/com.example.project.myapplication E/A﹕ 05:51:00
01-11 02:00:09.900 2872-2872/com.example.project.myapplication E/D﹕ 06:51:00
看起来它只检索一行数据...
最佳答案
type org.json.JSONArray cannot be converted to JSONObject
表示响应 JSON 字符串包含 JSONObject
的 JSONArray
。因此,将 JSON String 转换为 JSONArray,然后迭代以从中获取所有 JSONObject,如下所示:
JSONArray array=new JSONArray(json);
JSONObject jsonObject = array.getJSONObject(array.length()-1);
String MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
String MaXtimeOut=jsonObject.optString(Configs.TAG_OUT);
Log.e("A",MiNtimeIn);
Log.e("S", MaXtimeOut);
关于php - 如何获取MySQL表中的最小ID值和最大ID值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707414/