【问题标题】:No data retrieved from MySQL to android listView by given ID通过给定 ID 没有从 MySQL 检索到 android listView 的数据
【发布时间】:2016-04-06 11:15:18
【问题描述】:

我有一个表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";

【问题讨论】:

    标签: php android mysql listview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      请求参数区分大小写

      在 PHP 中,您检索 ID 全部大写 $id=$_GET['ID'];,但在 android 中您发送 detailsRetrieve.php?id="+ID); 其中 id 是小写的。

      有趣的是,您的变量名称已经颠倒了这种模式。

      将其更改为 id 的相同拼写,您会看到一些东西。

      新年快乐。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-23
        • 2021-01-03
        • 2012-04-07
        相关资源
        最近更新 更多