您好,我正在尝试从此网址解析JSON:
并将此数据输入到列表视图中 但是,我的列表视图没有显示任何内容:
这是我的代码:
PopulateLockerList.java
public class PopulateLockerList extends Activity {
// Used to make the URL to call for JSON data
String baseURL = "http://ec2-54-254-178-192.ap-southeast-1.compute.amazonaws.com:8080/LockerManagementSystem/rest/lockerservice/lockers?type=small&type=medium&building=SIS&building=SOE/SOSS&status=working&level=2&level=3";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lockerInfoList = new ArrayList<HashMap<String, String>>();
inputList = new ArrayList<String>();
setContentView(R.layout.locker_list_main);
final String displayLockerURL = baseURL;
new MyAsyncTask().execute(displayLockerURL);
}
protected List<String> doInBackground(String... arg0) {
// Holds Key Value pairs from a JSON source
JSONObject jsonObject;
JSONArray JsonlockerList = null;
try {
// Get the root JSONObject
jsonObject = new JSONObject(result);
// Get the JSON object named data
JSONObject dataJSONObject = jsonObject.getJSONObject("data");
// Getting JSON Array node
JsonlockerList = dataJSONObject.getJSONArray("data");
// JsonlockerList = jsonObject.getJSONArray("data");
// looping through All Lockers details
for (int i = 0; i < JsonlockerList.length(); i++) {
JSONObject jsonLockerObject = JsonlockerList.getJSONObject(i);
String lockerid = jsonLockerObject.getString("locker_id");
// Cluster node is a JSON object
JSONObject jsoncluster = jsonLockerObject
.getJSONObject("cluster");
String cluster_id = jsoncluster.getString("cluster_id");
// building node is a JSON object
JSONObject jsonbuilding = jsoncluster.getJSONObject("building");
String buildingID = jsonbuilding.getString("building_id");
String buildingName = jsonbuilding.getString("building_name");
String level = jsoncluster.getString("level");
HashMap<String, String> lockerRow = new HashMap<String, String>();
// / adding each child node to HashMap key => value
lockerRow.put("lockerid", lockerid);
lockerRow.put("cluster_id", cluster_id);
lockerRow.put("buildingID", buildingID);
lockerRow.put("buildingName", buildingName);
lockerRow.put("level", level);
// Adding lockerrow to lockerlist
lockerInfoList.add(lockerRow);
}// end of for loop
} catch (Exception e) {
e.printStackTrace();
}
return returnList;
}
protected void onPostExecute(List<HashMap<String, String>> returnList) {
// Each row in the list stores country name, currency and flag
List<HashMap<String, String>> aList = new
ArrayList<HashMap<String, String>>();
for (int i = 0; i < returnList.size(); i++) {
HashMap<String, String> hm = (HashMap<String,
String>) returnList.get(i);
// hm.put("txt", "Locker No : " + resultList.get(i));
hm.put("lockerid", "Locker ID : " + hm.get("lockerid"));
hm.put("cluster_id", "cluster_id : " + hm.get("cluster_id"));
hm.put("buildingID", "buildingID : " + hm.get("buildingID"));
hm.put("buildingName", "buildingName : " +
hm.get("buildingName"));
hm.put("level", "level : " + hm.get("level"));
hm.put("flag", Integer.toString(flags[0]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "lockerid", "cluster_id", "buildingID",
"buildingName", "level", "flag" };
// Ids of views in listview_layout
int[] to = { R.id.flag, R.id.lockerid, R.id.cluster_id,
R.id.buildingID, R.id.buildingName, R.id.level, R.id.flag };
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.locker_row, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.androidlist);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
请帮帮我
答案 0 :(得分:0)
试试这个..
除了以下几行之外,你所做的一切都是正确的
jsonObject = new JSONObject(result);
// Getting JSON Array node
JsonlockerList = jsonObject.getJSONArray("data");
只需删除JSONObject dataJSONObject = jsonObject.getJSONObject("data");
并更改为JsonlockerList = jsonObject.getJSONArray("data");
,因为数据为JSONArray
。