【问题标题】:No data retrieved from MySQL to Android ListView没有从 MySQL 检索到 Android ListView 的数据
【发布时间】:2015-12-31 10:03:15
【问题描述】:

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

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

检索信息

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

?>

浏览器

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>

【问题讨论】:

  • 嗨。我有几个问题。首先检查您从服务器获得的响应。下一个检查天气是否将命中服务器。下一个更改本地 ip 并保持全局 ip跨度>
  • 有没有我可以检查是否有数据检索到showlist()?
  • 在浏览器上打开 php 文件以查看它产生的输出,您也可以使用 POSTman chrome 扩展来检查请求/响应
  • 同样在 Android 中,您可以使用 EntityUtil 类作为 String responseBody = EntityUtils.toString(response.getEntity()); 读取一行代码中的响应。这将比使用输入流和使用循环更好
  • 数据显示在浏览器上

标签: php android mysql listview


【解决方案1】:

在您的服务器上使用“echo”运算符代替打印(或使用打印)

【讨论】:

  • 我已更改为echo json_encode(array("result"=&gt;$result));,但仍然没有显示数据:/
【解决方案2】:

使用回显操作符而不是打印。

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      感谢那些试图帮助我的人。终于解决了。

      Config.TAG.xxx 应始终与列名相同。如果该列无法检索数据 name 和 TAG 不一样...

      【讨论】:

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