我需要從JSON數組我的國家的名字,但是當我試圖代碼的不顯示任何.. 這是JSON輸出如何從android json數組中獲取Json值?
{
"status_code": 200,
"status": "OK",
"status_message": "Success",
"country_details": [
{
"country_code": "AF",
"country_name": "Afghanistan",
"country_iso": "AFG",
"country_flag": "http://..../img/countryflags/128x128/af.png",
"calling_code": "93",
"fancier_count": 2
},
在這裏,我只想country_name
但是當我試圖此代碼..它沒有顯示我什麼.. 這裏是我的代碼:
protected Void doInBackground(Void... params) {
String result = "";
try {
list.add("Select Country");
Thread.sleep(2000);
String data = (URLEncoder.encode("dest", "UTF-8") + "=" + URLEncoder.encode("destination", "UTF-8"));
URLConnection conn = new URL("http://.../api/getCountries").openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = rd.readLine();
if (line == null) {
wr.close();
rd.close();
//return Boolean.valueOf(true);
} else {
result += line;
}
} catch (Exception e2) {
e2.printStackTrace();
}
//parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
// add interviewee name to arraylist
list.add(jsonObject.getString("country_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(PMRegisterActivity.this, "Please wait...", "Fetching data", true, false);
list = new ArrayList<>();
}
我知道我缺少這個東西......但沒有得到什麼呢?
確定讓我來試試.. –
確定THX其工作 –
@ z.al請接受回答它是否工作。 –