php - 没有通过给定 ID 从 MySQL 检索到 android listView 的数据

标签 php android mysql listview

我有一个表 workDetails,如下图所示。

enter image description here

现在我想检索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();
        }

    }

详细信息检索

<?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";

最佳答案

您的$id = $_GET["ID"] 有错误。尝试将其更改为 $id=$_GET["id"]。您正在从 detailsRetrieve 检索,但您的文件是 detailsRetrieved。如果这只是打印错误,请忽略。

关于php - 没有通过给定 ID 从 MySQL 检索到 android listView 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564100/

相关文章:

准备好的语句中的 PHP MySQLi 查询

Android:谁启动 Activity

android - AudioFlinger 无法创建音轨,状态 : -12; Error creating AudioTrack

mysql - 枢。使用行作为列名并填充记录

mysql - 从作为服务器的 phpmyadmin 转移到作为服务器的谷歌云平台?

javascript - 如何只输出一条SQL结果?

php - 如何使用 cake php 或 php 在单个查询中同时获得喜欢和不喜欢的帖子计数

php - .htaccess 中的 HeaderName 不使用 .php 扩展名

php - eSELECTplus API 集成问题

Android 自定义键盘,无法实例化以下类 :