如何使用 volley 的 GET 方法检索数组中的数组?
How to retrieve array in array using GET methos of volley?
我正在使用 Rest Countries API 检索语言数据,https://restcountries.eu/rest/v2/all..但是语言没有显示,因为数据在数组中。
这是我使用Method.Get
检索数据的代码
private void getCountriesList() {
String url = "https://restcountries.eu/rest/v2/all";
StringRequest request = new StringRequest(
Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(!response.isEmpty()) {
Gson gson = new Gson();
JSONObject json = null;
// array of country
LanguageModel[] countries = gson.fromJson(response, LanguageModel[].class);
// add it to adapter
for(LanguageModel country: countries) {
mAdapter.addItem(country);
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.getLocalizedMessage());
}
}
);
Volley.newRequestQueue(getApplicationContext()).add(request);
}
这是模型。当我检索国家名称时代码成功,但当我检索语言数据时代码失败
public class CountryModel {
private String languages;
public String getLanguages() {
return languages;
}
public void setLanguages(String languages) {
this.languages = languages;
}}
我用下面的代码得到它:
if (!response.isEmpty()) {
Gson gson = new Gson();
try {
JSONArray mainArray = new JSONArray(response);
for (int i = 0; i < mainArray.length(); i++) {
JSONObject countryObj = mainArray.getJSONObject(i);
JSONArray languageArray = countryObj.getJSONArray("languages");
List<LanguageModel> languageModels = gson.fromJson(languageArray.toString(), new TypeToken<List<LanguageModel>>() {
}.getType());
// add it to adapter
for (LanguageModel languageModel : languageModels) {
//mAdapter.addItem(country);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// array of country
}
如果您仍然遇到错误,请告诉我。
编辑:
但是如何同时获取国家名称和语言?
您必须按如下方式编辑您的 CountryModel
:
public class CountryModel {
@SerializedName("name")
String countryName;
@SerializedName("languages")
ArrayList<LanguageModel> languages;
public CountryModel() {
}
}
和LanguageModel
如下:
class LanguageModel {
@SerializedName("iso639_1")
String iso1;
@SerializedName("iso639_2")
String iso2;
@SerializedName("name")
String name;
@SerializedName("nativeName")
String nativeName;
public LanguageModel() {
}
}
现在解析的最后变化:
if (!response.isEmpty()) {
Gson gson = new Gson();
try {
JSONArray mainArray = new JSONArray(response);
List<CountryModel> countryList = gson.fromJson(mainArray.toString(), new TypeToken<List<CountryModel>>() {
}.getType());
//do something with your country list
Log.d("R-Countries", countryList.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
虽然这个答案有效,但您应该看到以下内容 link 以了解我在这里所做的事情。
http://www.studytrails.com/java/json/java-google-json-introduction/
我正在使用 Rest Countries API 检索语言数据,https://restcountries.eu/rest/v2/all..但是语言没有显示,因为数据在数组中。
这是我使用Method.Get
检索数据的代码 private void getCountriesList() {
String url = "https://restcountries.eu/rest/v2/all";
StringRequest request = new StringRequest(
Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(!response.isEmpty()) {
Gson gson = new Gson();
JSONObject json = null;
// array of country
LanguageModel[] countries = gson.fromJson(response, LanguageModel[].class);
// add it to adapter
for(LanguageModel country: countries) {
mAdapter.addItem(country);
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.getLocalizedMessage());
}
}
);
Volley.newRequestQueue(getApplicationContext()).add(request);
}
这是模型。当我检索国家名称时代码成功,但当我检索语言数据时代码失败
public class CountryModel {
private String languages;
public String getLanguages() {
return languages;
}
public void setLanguages(String languages) {
this.languages = languages;
}}
我用下面的代码得到它:
if (!response.isEmpty()) {
Gson gson = new Gson();
try {
JSONArray mainArray = new JSONArray(response);
for (int i = 0; i < mainArray.length(); i++) {
JSONObject countryObj = mainArray.getJSONObject(i);
JSONArray languageArray = countryObj.getJSONArray("languages");
List<LanguageModel> languageModels = gson.fromJson(languageArray.toString(), new TypeToken<List<LanguageModel>>() {
}.getType());
// add it to adapter
for (LanguageModel languageModel : languageModels) {
//mAdapter.addItem(country);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// array of country
}
如果您仍然遇到错误,请告诉我。
编辑:
但是如何同时获取国家名称和语言?
您必须按如下方式编辑您的 CountryModel
:
public class CountryModel {
@SerializedName("name")
String countryName;
@SerializedName("languages")
ArrayList<LanguageModel> languages;
public CountryModel() {
}
}
和LanguageModel
如下:
class LanguageModel {
@SerializedName("iso639_1")
String iso1;
@SerializedName("iso639_2")
String iso2;
@SerializedName("name")
String name;
@SerializedName("nativeName")
String nativeName;
public LanguageModel() {
}
}
现在解析的最后变化:
if (!response.isEmpty()) {
Gson gson = new Gson();
try {
JSONArray mainArray = new JSONArray(response);
List<CountryModel> countryList = gson.fromJson(mainArray.toString(), new TypeToken<List<CountryModel>>() {
}.getType());
//do something with your country list
Log.d("R-Countries", countryList.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
虽然这个答案有效,但您应该看到以下内容 link 以了解我在这里所做的事情。
http://www.studytrails.com/java/json/java-google-json-introduction/