我有一个表workDetails,如下图所示。
现在我想检索twd等于1的两个数据并将它们加载到android listView中。我不确定我错过了什么,因为没有检索到数据。
public void BuildEditDetails(final String ID) { // It holds 1
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.1.7/Android/CRUD/detailsRetrieve.php?id="+ID);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
details= jsonObj.getJSONArray(Config.TAG_RESULTS);
for(int i=0;i<details.length();i++){
JSONObject c = details.getJSONObject(i);
String project = c.getString(Config.TAG_PROJECT);
String description = c.getString(Config.TAG_WORKDESCRIPTION);
int percentage = c.getInt(Config.TAG_PERCENTAGE);
String in=c.getString(Config.TAG_IN);
String out=c.getString(Config.TAG_OUT);
int ID=c.getInt(Config.TAG_ID);
HashMap<String,String> info = new HashMap<String,String>();
info.put(Config.TAG_PROJECT, project);
info.put(Config.TAG_WORKDESCRIPTION, description);
info.put(Config.TAG_PERCENTAGE,percentage+"");
info.put(Config.TAG_IN,in);
info.put(Config.TAG_OUT,out);
info.put(Config.TAG_ID,ID+"");
EditDetails.add(info);
}
ListAdapter adapter = new SimpleAdapter(
getActivity(),EditDetails, R.layout.retrieve_details,
new String[]{Config.TAG_PROJECT,Config.TAG_WORKDESCRIPTION,Config.TAG_PERCENTAGE,Config.TAG_IN,Config.TAG_OUT},
new int[]{R.id.Project,R.id.Description,R.id.in, R.id.out,R.id.Percentage}
);
listViewUpdate.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
detailsRetrieved
<?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');
$id=$_GET['ID'];
$sql = "select * from work_details WHERE twd= '". $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], 'twd'=>$row[6]));
}
echo (json_encode(array("result"=>$result)));
mysqli_close($con);
?>
配置
public static final String TAG_RESULTS="result";
public static final String TAG_ID = "id";
public static final String TAG_PROJECT="project";
public static final String TAG_WORKDESCRIPTION="work_description";
public static final String TAG_PERCENTAGE="percentage";
public static final String TAG_IN="timeIn";
public static final String TAG_OUT="timeOut";
答案 0 :(得分:3)
您的$id = $_GET["ID"]
出错。尝试将其更改为$id=$_GET["id"]
。您正在从detailsRetrieve
检索,但您的文件是detailsRetrieved
。如果那只是印刷错误,请忽略。
答案 1 :(得分:1)
请求参数区分大小写
在PHP中,您检索全部大写$id=$_GET['ID'];
的ID,但在android中您发送detailsRetrieve.php?id="+ID);
其中id是小写的。
虽然您的变量名称已反转该模式,但很有趣。
将其更改为相同的ID拼写,您会看到一些内容。
新年快乐。