没有由下式给出ID从MySQL中检索到Android的ListView数据 [英] No data retrieved from MySQL to android listView by given ID

查看:313
本文介绍了没有由下式给出ID从MySQL中检索到Android的ListView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表workDetails,如下面的图像。

在这里输入的形象描述

现在我要检索的两个数据哪个TWD等于1,并将它们加载到Android的ListView控件。我不知道我已经错过了,因为没有检索到的数据。

 公共无效BuildEditDetails(最后弦乐ID){//它拥有1        类GetDataJSON扩展的AsyncTask<弦乐,太虚,字符串> {            @覆盖
            保护字符串doInBackground(字符串... PARAMS){
                DefaultHttpClient的HttpClient =新DefaultHttpClient(新BasicHttpParams());
                HttpPost httppost =新HttpPost(http://192.168.1.7/Android/CRUD/detailsRetrieve.php?id=+ ID);
                //取决于你的Web服务
                httppost.setHeader(内容类型,应用/ JSON);                为InputStream的InputStream = NULL;
                字符串结果= NULL;
                尝试{
                    HTT presponse响应= httpclient.execute(httppost);
                    HttpEntity实体= response.getEntity();                    的InputStream = entity.getContent();
                    // JSON默认为UTF-8
                    读者的BufferedReader =新的BufferedReader(新的InputStreamReader(InputStream中,UTF-8),8);
                    StringBuilder的SB =新的StringBuilder();                    串线= NULL;
                    而((行= reader.readLine())!= NULL)
                    {
                        sb.append(行+\\ n);
                    }
                    结果= sb.toString();
                }赶上(例外五){
                    //哎呀
                }
                最后{
                    尝试{如果(InputStream的!= NULL)inputStream.close();}赶上(例外挤){}
                }
                返回结果;
            }            @覆盖
            保护无效onPostExecute(字符串结果){
                myJSON =结果;
                showList();
            }
        }
        GetDataJSON G =新GetDataJSON();
        g.execute();
    }    保护无效showList(){
        尝试{
            JSONObject的jsonObj =新的JSONObject(myJSON);
            细节= jsonObj.getJSONArray(Config.TAG_RESULTS);            的for(int i = 0; I< details.length();我++){
                JSONObject的C = details.getJSONObject(I)
                字符串项目= c.getString(Config.TAG_PROJECT);
                字符串描述= c.getString(Config.TAG_WORKDESCRIPTION);
                INT百分比= c.getInt(Config.TAG_PERCENTAGE);
                在= c.getString(Config.TAG_IN)字符串;
                串出= c.getString(Config.TAG_OUT);
                INT ID = c.getInt(Config.TAG_ID);
                HashMap的<字符串,字符串>信息=新的HashMap<字符串,字符串>();
                info.put(Config.TAG_PROJECT,项目);
                info.put(Config.TAG_WORKDESCRIPTION,说明);
                info.put(Config.TAG_PERCENTAGE,百分比+);
                info.put(Config.TAG_IN,在);
                info.put(Config.TAG_OUT,出);
                info.put(Config.TAG_ID,ID +);                EditDetails.add(信息);
            }            ListAdapter适配器=新SimpleAdapter(
                    getActivity(),EditDetails,R.layout.retrieve_details,
                    新String[]{Config.TAG_PROJECT,Config.TAG_WORKDESCRIPTION,Config.TAG_PERCENTAGE,Config.TAG_IN,Config.TAG_OUT},
                    新的INT [] {R.id.Project,R.id.Description,R.id.in,R.id.out,R.id.Percentage}
            );            listViewUpdate.setAdapter(适配器);        }赶上(JSONException E){
            e.printStackTrace();
        }    }

detailsRetrieved

 < PHP
   定义(HOST,127.0.0.1:3307');
   定义('用户','根');
   定义('PASS','');
   定义(DB,androiddb');   $ CON = mysqli_connect(HOST,USER,PASS,DB)或死亡(无法连接);   $ n = $ _ GET ['身份证'];   $的SQL =从work_details选择*其中TWD ='。 $ id为'。   $解析度= mysqli_query($ CON,$ SQL);   $结果=阵列();   而($行= mysqli_fetch_array($水库)){
       array_push($result,array('id'=>$row[0],'project'=>$row[1],'work_description'=>$row[2],'percentage'=>$row[3],'timeIn'=>$row[4],
      超时=> $行[5],'TWD => $行[6]));
   }   回声(json_en code(阵列(结果=> $结果)));   mysqli_close($ CON);?>

配置

 公共静态最后弦乐TAG_RESULTS =结果;
公共静态最后弦乐TAG_ID =ID;
公共静态最后弦乐TAG_PROJECT =工程;
公共静态最后弦乐TAG_WORKDESCRIPTION =work_description;
公共静态最后弦乐TAG_PERCENTAGE =百分比;
公共静态最后弦乐TAG_IN =timeIn;
公共静态最后弦乐TAG_OUT =超时;


解决方案

有与您的的$ id = $ _GET错误[ID] 。试着将其更改为的$ id = $ _ GET [身份证] 。并正在从 detailsRetrieve 检索,但是你的文件是 detailsRetrieved 。如果忽略这仅仅是一个拼写错误。

I have a table workDetails as shown in image below.

Now I want to retrieve the two data which twd is equal to 1 and load them into android listView. I not sure what have I missed out, as there are no data retrieved.

  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);

?>

Config

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

解决方案

There is an error with your $id = $_GET["ID"]. Try changing it to $id=$_GET["id"]. And you are retrieving from detailsRetrieve, yet your file is detailsRetrieved. Ignore if that is only a typographical error.

这篇关于没有由下式给出ID从MySQL中检索到Android的ListView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆