gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:52:11 25 4
gpt4 key购买 nike

我正在尝试将数据从 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);

?>

浏览器

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>

最佳答案

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

关于php - 没有从 MySQL 检索到 Android ListView 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34544643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com