没有从MySQL检索到Android ListView的数据

时间:2015-12-31 10:03:16

标签: php android mysql listview

我正在尝试将MySQL服务器的数据显示到android,但没有显示数据。

数据无法检索到Android ListView。但我可以看到 数据显示在我的本地主机上。

RetrieveInformation

 String myJSON;
    JSONArray information = null;
    ArrayList<HashMap<String, String>> infoList;

   infoList = new ArrayList<HashMap<String, String>>();
   listView = (ListView) findViewById(R.id.listView2);

       public void getData() {
            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.107.115:80/Android/CRUD/retrieveInformation.php");

                    // 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);
                information = jsonObj.getJSONArray(Config.TAG_RESULTS);

                for(int i=0;i<information.length();i++){
                    JSONObject c = information.getJSONObject(i);
                    String date = c.getString(Config.TAG_DATE);
                    Toast.makeText(getApplicationContext(),date,Toast.LENGTH_LONG).show();
                    String timeIn = c.getString(Config.TAG_TiME_IN);
                    Toast.makeText(getApplicationContext(),timeIn,Toast.LENGTH_LONG).show();
                    String timeOut = c.getString(Config.TAG_TIME_OUT);

                    HashMap<String,String> info = new HashMap<String,String>();
                    info.put(Config.TAG_DATE, date);
                   info.put(Config.TAG_TiME_IN, timeIn);
                   info.put(Config.TAG_TIME_OUT,timeOut);

                    infoList.add(info);
                }

                ListAdapter adapter = new SimpleAdapter(
                        HomePage.this, infoList, R.layout.retrieve_data,
                        new String[]{Config.TAG_DATE,Config.TAG_TiME_IN,Config.TAG_TIME_OUT},
                        new int[]{R.id.date,R.id.timeIn,R.id.timeOut}
                );

                listView.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

retrieveInformation.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');

  $sql = "select * from information";

  $res = mysqli_query($con,$sql);

  $result=array();

  while($row=mysqli_fetch_array($res)){
      array_push($result,array('id'=>$row[0],'name'=>$row[1],'weather'=>$row[2],'date'=>$row[3],'status'=>$row[4],
      'time_in'=>$row[5], 'time_out'=>$row[6]));
  }

 echo (json_encode(array("result"=>$result)));

mysqli_close($con);

?>

浏览器

enter image description here

retrieve_data.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="start"
    android:background="@android:color/white"
    android:padding="12dp">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"
        android:layout_centerVertical="true"
        android:paddingLeft="6dp"
        android:orientation="vertical"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/l21"
            android:orientation="horizontal"
            android:maxLines="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="210dp"
                android:textSize="10dp"
                android:id="@+id/date"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll2"
            android:layout_below="@+id/l21"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/TimeIn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="@android:color/secondary_text_dark_nodisable"
                android:text="Time In     : "
                android:maxLines="1"
                android:ellipsize="end" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/timeIn" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll3"
            android:layout_below="@+id/ll2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="@android:color/secondary_text_dark_nodisable"
                android:text="Time Out  : "
                android:maxLines="1"
                android:layout_below="@+id/TimeIn"
                android:id="@+id/TimeOut" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/timeOut"
                android:layout_toRightOf="@+id/TimeOut"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

在服务器上使用“echo”操作符,而不是打印(或使用打印)

答案 1 :(得分:0)

使用 echo operator 而非打印。

echo没有返回值,而print的返回值为1,因此可以在表达式中使用它。 echo可以采用多个参数(尽管这种用法很少见),而print只能使用一个参数。回声比打印快一点。

答案 2 :(得分:0)

检查您是否在执行请求时获得正常的服务器响应代码,或者从500系列HTTP代码获取错误。尝试通过从代码的开头打印一些变量并在客户端检查响应来调试php代码。

答案 3 :(得分:0)

感谢那些试图帮助我的人。最后我解决了。

Config.TAG.xxx应始终与列名相同。如果列,则无法检索数据 名称和标签不同......