gpt4 book ai didi

java - 使用 JSON 获取数据

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

我正在编写一个应用程序,其中我使用 JSON 将数据提取到 ListView 中,并且我已经提取了第一级 (eventName) 的数据,但在提取第二级 (angelName) 时获得空白 Activity ...

JSON:-

[
{
"eventName" : "Honda Motors",
"angelList" : [
{
"angelID": "1",
"angelName": "Angel A"
},
{
"angelID": "2",
"angelName": "Angel B"
}
]
}
]

简而言之,无法获取AngelName,

AngelsActivity.java:-

public class AngelsActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;

// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> angelsList;

// albums JSONArray
JSONArray arrayAngels = null;

// Category name
String category_Name;

// ALL JSON node names
private static final String ANGEL_NAME = "angelName";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);

cd = new ConnectionDetector(getApplicationContext());

// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AngelsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}

// Hashmap for ListView
angelsList = new ArrayList<HashMap<String, String>>();

// Loading Albums JSON in Background Thread
new LoadAlbums().execute();

// get listview
ListView lv = getListView();

/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {

}
});
}

/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AngelsActivity.this);
pDialog.setMessage("Listing Angels ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

/**
* getting Categories JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_CATEGORIES, "GET",
params);

try {
arrayAngels = new JSONArray(json);

if (arrayAngels != null) {

// looping through All albums
for (int i = 0; i < arrayAngels.length(); i++) {
JSONObject c = arrayAngels.getJSONObject(i);

// Storing each json item values in variable
String name = c.getString(ANGEL_NAME);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(ANGEL_NAME, name);

// adding HashList to ArrayList
angelsList.add(map);
}
}else{
Log.d("Angels: ", "null");
}

} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AngelsActivity.this, angelsList,
R.layout.list_item,
new String[] {ANGEL_NAME},
new int[] {R.id.category_name });

// updating listview
setListAdapter(adapter);
}
});
}
}
}

最佳答案

试试这个,

protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_CATEGORIES, "GET",
params);

try {
arrayAngels = new JSONArray(json).getJSONObject(0).getJSONArray("angelList");

if (arrayAngels != null) {

// looping through All albums
for (int i = 0; i < arrayAngels.length(); i++) {
JSONObject c = arrayAngels.getJSONObject(i);

// Storing each json item values in variable
String name = c.getString(ANGEL_NAME);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(ANGEL_NAME, name);

// adding HashList to ArrayList
angelsList.add(map);
}
}else{
Log.d("Angels: ", "null");
}

} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

关于java - 使用 JSON 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20192223/

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