没有从MySQL中检索到Android的ListView数据 [英] No data retrieved from MySQL to Android ListView
本文介绍了没有从MySQL中检索到Android的ListView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示来自的MySQL
服务器数据到Android,但没有数据显示。
数据不能够检索出到Android 的ListView
。但我可以看到
数据显示得到我的本地主机。
RetrieveInformation
字符串myJSON;
JSONArray信息= NULL;
ArrayList的<&HashMap的LT;字符串,字符串>> infoList; infoList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
ListView控件=(ListView控件)findViewById(R.id.listView2); 公共无效的getData(){
类GetDataJSON扩展的AsyncTask<弦乐,太虚,字符串> { @覆盖
保护字符串doInBackground(字符串... PARAMS){
DefaultHttpClient的HttpClient =新DefaultHttpClient(新BasicHttpParams());
HttpPost httppost =新HttpPost(http://192.168.107.115:80/Android/CRUD/retrieveInformation.php); //取决于你的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< information.length();我++){
JSONObject的C = information.getJSONObject(I)
字符串日期= c.getString(Config.TAG_DATE);
Toast.makeText(getApplicationContext(),日期,Toast.LENGTH_LONG).show();
字符串timeIn = c.getString(Config.TAG_TiME_IN);
Toast.makeText(getApplicationContext(),timeIn,Toast.LENGTH_LONG).show();
字符串TIMEOUT = c.getString(Config.TAG_TIME_OUT); HashMap的<字符串,字符串>信息=新的HashMap<字符串,字符串>();
info.put(Config.TAG_DATE,日期);
info.put(Config.TAG_TiME_IN,timeIn);
info.put(Config.TAG_TIME_OUT,超时); infoList.add(信息);
} ListAdapter适配器=新SimpleAdapter(
HomePage.this,infoList,R.layout.retrieve_data,
新的String [] {Config.TAG_DATE,Config.TAG_TiME_IN,Config.TAG_TIME_OUT},
新的INT [] {R.id.date,R.id.timeIn,R.id.timeOut}
); listView.setAdapter(适配器); }赶上(JSONException E){
e.printStackTrace();
} }
retrieveInformation.php
< PHP
定义(HOST,127.0.0.1:3307');
定义('用户','根');
定义('PASS','');
定义(DB,androiddb'); $ CON = mysqli_connect(HOST,USER,PASS,DB)或死亡(无法连接); $的SQL =选择*信息; $解析度= mysqli_query($ CON,$ SQL); $结果=阵列(); 而($行= mysqli_fetch_array($水库)){
array_push($result,array('id'=>$row[0],'name'=>$row[1],'weather'=>$row[2],'date'=>$row[3],'status'=>$row[4],
'time_in'=> $行[5],'TIME_OUT'=> $行[6]));
} 回声(json_en code(阵列(结果=> $结果)));mysqli_close($ CON);?>
浏览器
retrieve_data.xml
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:重力=开始
机器人:背景=@机器人:彩色/白
机器人:填充=12dp>
< RelativeLayout的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ + ID /图像
机器人:layout_centerVertical =真
机器人:paddingLeft =6DP
机器人:方向=垂直
机器人:layout_alignParentRight =真
机器人:layout_alignParentEnd =真正的>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / L21
机器人:方向=横向
机器人:MAXLINES =1>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingLeft =210dp
机器人:TEXTSIZE =10dp
机器人:ID =@ + ID /日期/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / [112
机器人:layout_below =@ + ID / L21
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<的TextView
机器人:ID =@ + ID / TimeIn
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =14sp
机器人:文字颜色=@机器人:彩色/ secondary_text_dark_nodisable
机器人:文字=时间:
机器人:MAXLINES =1
机器人:ellipsize =结束/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / timeIn/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / 113
机器人:layout_below =@ + ID / [112
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =14sp
机器人:文字颜色=@机器人:彩色/ secondary_text_dark_nodisable
机器人:文字=超时
机器人:MAXLINES =1
机器人:layout_below =@ + ID / TimeIn
机器人:ID =@ + ID /超时/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /超时
机器人:layout_toRightOf =@ + ID /超时/>
< / LinearLayout中>
< / RelativeLayout的>
< / RelativeLayout的>
解决方案
感谢那些谁试图帮助我。最后,我解决了它。
的 Config.TAG.xxx
应始终列名相同。数据不能如果列检索
名和TAG是不同的...
I'm trying to display data from MySQL
server to android, but no data are shown.
Data are not able to retrieve out to Android ListView
. But I can see
data get displayed on my localhost.
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);
?>
Browser
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>
解决方案
Thanks for those who are trying to help me. Finally I solved it.
The Config.TAG.xxx
should always same with the column name. Data cannot retrieved if the column
name and TAG are different...
这篇关于没有从MySQL中检索到Android的ListView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文