Php 回收器视图可以';不显示mysql数据
请帮我查一下这个密码。 我想显示mysql中的数据并将其显示给android。我在mysql中插入数据并在android上显示mysql中有多少数据,这样我就知道我的android应用程序是否可以从mysql读取我的所有数据,但当我运行android应用程序时,它说没有数据。请帮帮我 Android代码Php 回收器视图可以';不显示mysql数据,php,android,mysql,android-recyclerview,Php,Android,Mysql,Android Recyclerview,请帮我查一下这个密码。 我想显示mysql中的数据并将其显示给android。我在mysql中插入数据并在android上显示mysql中有多少数据,这样我就知道我的android应用程序是否可以从mysql读取我的所有数据,但当我运行android应用程序时,它说没有数据。请帮帮我 Android代码 data_list = new ArrayList<>(); load_data_from_server(); gridLayoutManager = new Gri
data_list = new ArrayList<>();
load_data_from_server();
gridLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new CustomAdapter(this, data_list);
recyclerView.setAdapter(adapter);
if (adapter.getItemCount() == 0){
Toast.makeText(this, "Empty", Toast.LENGTH_SHORT).show();
}
private void load_data_from_server() {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.43.147/webapp/travelplanning.php")
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i = 0; i < array.length(); i++){
JSONObject object = array.getJSONObject(i);
MyData data = new MyData(object.getInt("ID"), object.getString("AttractionName"), object.getString("Location"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
};
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".travelplanning"
android:background="@drawable/img_banner">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="60dp"
android:background="@drawable/square"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recTravel">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
<EditText
android:id="@+id/etSearchAttraction"
android:layout_width="0dp"
android:layout_height="38dp"
android:layout_marginStart="10dp"
android:layout_marginTop="72dp"
android:autofillHints=""
android:background="@drawable/searchdestination"
android:drawableStart="@drawable/icon_search1"
android:ems="10"
android:hint="@string/etsearchdestination"
android:inputType="textPersonName"
android:paddingStart="10dp"
app:layout_constraintEnd_toStartOf="@+id/btnAddAttraction"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:focusable="false"/>
<Button
android:id="@+id/btnAddAttraction"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/icon_plus"
app:layout_constraintBaseline_toBaselineOf="@+id/etSearchAttraction"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/etSearchAttraction" />
<FrameLayout
android:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/navbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/nav_topborder"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="63dp"
android:orientation="horizontal"
android:paddingTop="1dp">
<Button
android:id="@+id/btnAttraction"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/navbar"
android:drawableTop="@drawable/icon_places"
android:paddingTop="7dp"
android:paddingBottom="18dp"
android:text="@string/attract"
android:textAllCaps="false"
android:textColor="#2d3e52"
android:textSize="9dp" />
<Button
android:id="@+id/btnTrips"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/navbar"
android:drawableTop="@drawable/icon_trips"
android:paddingTop="7dp"
android:paddingBottom="18dp"
android:text="@string/trips"
android:textAllCaps="false"
android:textColor="#A6A3A3"
android:textSize="9dp" />
<Button
android:id="@+id/btnWishlist"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/navbar"
android:drawableTop="@drawable/icon_wishlist"
android:paddingTop="7dp"
android:paddingBottom="18dp"
android:text="@string/wishlist"
android:textAllCaps="false"
android:textColor="#A6A3A3"
android:textSize="9dp" />
</LinearLayout>
</FrameLayout>
<ImageView
android:id="@+id/imgUser"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:src="@drawable/icon_mainuser"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_logowhite" />
卡片布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imgAttraction"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher_round"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="320dp"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvAttractionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attraction Name"
android:textSize="20dp"
android:textColor="#000000"/>
<TextView
android:id="@+id/tvLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:textSize="20dp"
android:textColor="#000000"/>
</LinearLayout>
<TextView
android:id="@+id/tvID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textSize="26dp"
android:textColor="#000000"
android:layout_marginTop="7dp"
android:textAlignment="textEnd"
android:visibility="invisible"/>
<TextView
android:id="@+id/tvRate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="Rating"
android:textAlignment="textEnd"
android:textColor="#000000"
android:textSize="26dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
PHP文件
<?php
require "init.php";
if(mysqli_connect_error($con))
{
echo "Failed to connect";
}
$query = "Select id, attractionname, location from attraction";
$result = mysqli_query($con, $query);
if($result){
while($row=mysqli_fetch_array($result)){
$flag[]=$row;
}
print(json_encode($flag));
}
mysqli_close($con);
?>
你不能这样做。因为,您不会在AsyncTask中更改ui操作。试试这个
private void load_data_from_server() {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.43.147/webapp/travelplanning.php")
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
List<MyData> list = new ArrayList<>();
for(int i = 0; i < array.length(); i++){
JSONObject object = array.getJSONObject(i);
MyData data = new MyData(object.getInt("ID"), object.getString("AttractionName"), object.getString("Location"));
list.add(data);
}
setListData(list);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
}
};
}
private void setListData(List<MyData> list) {
data_list.addAll(list);
adapter.notifyDataSetChanged();
}
private void从\u服务器()加载\u数据{
AsyncTask任务=新建AsyncTask(){
@凌驾
受保护的Void doInBackground(整数…整数){
OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder()
.url(“http://192.168.43.147/webapp/travelplanning.php")
.build();
试一试{
Response=client.newCall(request.execute();
JSONArray数组=新的JSONArray(response.body().string());
列表=新的ArrayList();
对于(int i=0;i
谢谢您的回复,但仍然是空的。我的mysql中有3个示例数据,但它没有显示在recycler视图中。您检查响应数据了吗?如果有数据,我们检查客户端(android)。
<?php
require "init.php";
if(mysqli_connect_error($con))
{
echo "Failed to connect";
}
$query = "Select id, attractionname, location from attraction";
$result = mysqli_query($con, $query);
if($result){
while($row=mysqli_fetch_array($result)){
$flag[]=$row;
}
print(json_encode($flag));
}
mysqli_close($con);
?>
private void load_data_from_server() {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.43.147/webapp/travelplanning.php")
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
List<MyData> list = new ArrayList<>();
for(int i = 0; i < array.length(); i++){
JSONObject object = array.getJSONObject(i);
MyData data = new MyData(object.getInt("ID"), object.getString("AttractionName"), object.getString("Location"));
list.add(data);
}
setListData(list);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
}
};
}
private void setListData(List<MyData> list) {
data_list.addAll(list);
adapter.notifyDataSetChanged();
}