Нет данных, полученных из MySQL в Android 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); ?> в <?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> 

используйте оператор «echo» на своем сервере вместо печати (или с печатью)

Используйте эхо-оператор, чем печать.

echo не имеет возвращаемого значения, а печать имеет возвращаемое значение 1, поэтому его можно использовать в выражениях. эхо может принимать несколько параметров (хотя такое использование встречается редко), в то время как печать может принимать только один аргумент. эхо немного быстрее, чем печать.

Проверьте, получаете ли вы обычный код ответа сервера при выполнении вашего запроса или ошибку из HTTP-кодов серии 500. Попробуйте отладить ваш php-код, напечатав некоторые переменные с начала кода и проверив ответы на стороне клиента.

Спасибо тем, кто пытается мне помочь. Наконец я решил это.

Config.TAG.xxx всегда должен Config.TAG.xxx с именем столбца. Данные не могут быть получены, если имя столбца и TAG отличаются друг от друга …