我是android studio的新手。目前,我正在尝试使用Volley从mysql中获取数据。不幸的是,数据只获取第一个ID而其他信息仍为空。我试图只获取ID并且它有效,它显示所有ID。但是,当我获取chapter_num和chapter_title时,输出为空。输出应该有ID
,chapter_num
,chapter_title
。
<?php
$db_name = "spmsejarahrevisionapp";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$res = mysqli_query($conn, "SELECT id, chapter_num, chapter_title FROM spmtopic WHERE form='4' ORDER BY chapter_num");
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array(
'id'=>$row[0],
'chapter_num'=>$row[1],
'chapter_title'=>$row[2]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($conn);
这是数据:
{
"result": [
{
"id": "3",
"chapter_num": "1",
"chapter_title": "Kemunculan Tamadun Awal Manusia"
},
{
"id": "4",
"chapter_num": "2",
"chapter_title": "Peningkatan Tamadun"
},
{
"id": "5",
"chapter_num": "3",
"chapter_title": "Tamadun Awal Asia Tenggara"
},
{
"id": "21",
"chapter_num": "4",
"chapter_title": "Kemunculan Tamadun Islam di Makkah"
},
{
"id": "6",
"chapter_num": "5",
"chapter_title": "Kerajaan Islam Di Madinah"
},
{
"id": "7",
"chapter_num": "6",
"chapter_title": "Kerajaan Islam dan Sumbangannya"
},
{
"id": "8",
"chapter_num": "7",
"chapter_title": "Islam di Asia Tenggara"
},
{
"id": "9",
"chapter_num": "8",
"chapter_title": "Pengaruh Islam di Malaysia"
},
{
"id": "10",
"chapter_num": "9",
"chapter_title": "Perkembangan di Eropah"
},
{
"id": "11",
"chapter_num": "10",
"chapter_title": "Dasar British dan Kesannya Terhadap Ekonomi Negara"
}
]
}
MainActivity.java
public class MMapf4Activity extends CodeReuse {
public static final String JSON_URL = "http://10.0.2.2/conn.php";
public ListView listView;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mmapf4);
barRule();
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_arrow_left_white);
sendRequest();
listView = (ListView) findViewById(R.id.listView);
}
private void sendRequest(){
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
StringRequest stringRequest = new StringRequest(JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
loading.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MMapf4Activity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.nums,ParseJSON.titles);
listView.setAdapter(cl);
}
}
ParseJson:
public class ParseJSON {
public static String[] ids;
public static String[] nums;
public static String[] titles;
public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_NUM = "nums";
public static final String KEY_TITLE = "titles";
private JSONArray users = null;
private String json;
public ParseJSON(String json){
this.json = json;
}
protected void parseJSON(){
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
ids = new String[users.length()];
nums = new String[users.length()];
titles = new String[users.length()];
for(int i=0;i < users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
nums[i] = jo.getString(KEY_NUM);
titles[i] = jo.getString(KEY_TITLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
CustomList.java
public class CustomList extends ArrayAdapter<String> {
private String[] ids;
private String[] nums;
private String[] titles;
private Activity context;
public CustomList(Activity context, String[] ids, String[] nums, String[] titles) {
super(context, R.layout.spmtopic_list, ids);
this.context = context;
this.ids = ids;
this.nums = nums;
this.titles = titles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.spmtopic_list, null, true);
TextView textViewId = (TextView) listViewItem.findViewById(R.id.id);
TextView textViewNum = (TextView) listViewItem.findViewById(R.id.chapter_num);
TextView textViewTitle = (TextView) listViewItem.findViewById(R.id.chapter_title);
textViewId.setText(ids[position]);
textViewNum.setText(nums[position]);
textViewTitle.setText(titles[position]);
return listViewItem;
}
}
spmtopic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/id"
android:text="New Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/chapter_num"
android:text="New Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/chapter_title"
android:text="New Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.foong.spmsejerahscore.MMapf4Activity">
<include
layout="@layout/app_bar_2_reuse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/include" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="507dp"
android:scrollbars="vertical"
android:layout_below="@+id/include"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
我的输出:
答案 0 :(得分:0)
不确定为什么你认为它卡住了,但你的JSON键不匹配,所以它可能在第一条消息后引发异常
public static final String KEY_ID = "id";
// these aren't in your data
public static final String KEY_NUM = "nums";
public static final String KEY_TITLE = "titles";
另外,适配器的推荐模式是
public class CustomList extends ArrayAdapter<Chapter> {
private Chapter[] chapters;
private Context context;
答案 1 :(得分:0)
public static final String KEY_ID = "id";
public static final String KEY_NUM = "nums";
public static final String KEY_TITLE = "titles";
使用
更改密钥public static final String KEY_ID = "id";
public static final String KEY_NUM = "chapter_num";
public static final String KEY_TITLE = "chapter_title";