使用 Android Volley 从站点获取一组对象?不是数组
Using Android Volley to get a group of objects from a site? not an array
所以在我目前正在学习的课程中,我被要求使用 Android Volley 来访问 https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson">this 并在应用程序中解析它们,这样我就可以将它们粘贴到文本视图中。
老实说,我现在才刚刚了解 JSON 和 Volley,所以我找不到答案的原因很可能是因为它太明显了,我只是想多了,但我可以'我不太清楚如何获取这些对象并将它们粘贴到 Java 数组列表中。它们没有被归类为 JSON 数组,所以当我尝试这样做时我的代码失败了,而且它们在 ID 或其他方面没有任何明显的组织,所以我不能只使用循环来单独访问它们(据我所知)因为我必须假设明天他们将拥有完全不同的 ID
这是我尝试解析的一些代码
public ArrayList<Quake> decodeMessage(String message) {
try {
Log.d(TAG, "Parsing: " + message);
JSONArray jArray;
jArray = new JSONArray(message);
for (int i=0;i<jArray.length();i++){
JSONObject jObject;
JSONArray jFeaturesArr;
JSONObject jPropertiesObj;
JSONObject jGeometryObj;
JSONArray jCoordinatesArr;
jObject = jArray.getJSONObject(i);
jFeaturesArr = jObject.getJSONArray(Quake.FEATURES);
jPropertiesObj=jFeaturesArr.getJSONObject(1);
jGeometryObj = jFeaturesArr.getJSONObject(2);
jCoordinatesArr = jGeometryObj.getJSONArray(Quake.COORDINATES);
quakes.add(new
Quake(jPropertiesObj.getLong(Quake.DATE),
jPropertiesObj.getString(Quake.PLACE),
jCoordinatesArr.getDouble(1),
jCoordinatesArr.getDouble(0),
jPropertiesObj.getDouble(Quake.MAGNITUDE),
jPropertiesObj.getString(Quake.DETAIL)));
}
} catch (Exception e) {
Log.e(TAG, "decodeMessage: exception during parsing");
e.printStackTrace();
return null;
}
return quakes;
}
之后我得到一个错误
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
等(仅指向我的解码方法和我声明 JSONArray 的行)
我的地震class
public class Quake {
private long id;
private Date date;
private String location;
private double longitude;
private double magnitude;
private double latitude;
private String detail;
private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
public Quake()
{
}
public Quake(Long date, String location, double latitude, double longitude, double magnitude, String detail) {
this.date = new Date(date);
this.location = location;
this.latitude = latitude;
this.longitude = longitude;
this.magnitude = magnitude;
this.detail = detail;
}
public String getExtraText()
{
String output = "An Earthquake occurred at the time " + sdf.format(date)+
"with a magnitude of "+ magnitude + " and longitude and latitude of "
+longitude + " and " + latitude + " respectively. Notably the location was
"+location+ ".";
return output;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getMagnitude() {
return magnitude;
}
public void setMagnitude(double magnitude) {
this.magnitude = magnitude;
}
@Override
public String toString() {
String output= "";
output+= sdf.format(date);
output += " "+ magnitude + " " + location;
return output;
}
public static final String DATE = "time";
public static final String PLACE = "place";
public static final String MAGNITUDE = "mag";
public static final String DETAIL = "detail";
public static final String LONGITUDE = "longitude";
public static final String LATITUDE = "latitude";
public static final String GEOMETRY = "geometry";
public static final String FEATURES = "features";
public static final String PROPERTIES = "properties";
public static final String COORDINATES = "coordinates";
}
请注意,我仍在从之前使用数据库的任务中转移过来(我现在不需要,但是你)
JSONObject jsonObject;
try {
jsonObject = new JSONObject(message);
JSONArray jsonArray = jsonObject.getJSONArray(Quake.FEATURES);
//Continue get remain information
} catch (JSONException e) {
e.printStackTrace();
}
试试这个。因为完整的消息是一个 json 对象,你需要获取 json 数组,键为 "features"
所以在我目前正在学习的课程中,我被要求使用 Android Volley 来访问 https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson">this 并在应用程序中解析它们,这样我就可以将它们粘贴到文本视图中。
老实说,我现在才刚刚了解 JSON 和 Volley,所以我找不到答案的原因很可能是因为它太明显了,我只是想多了,但我可以'我不太清楚如何获取这些对象并将它们粘贴到 Java 数组列表中。它们没有被归类为 JSON 数组,所以当我尝试这样做时我的代码失败了,而且它们在 ID 或其他方面没有任何明显的组织,所以我不能只使用循环来单独访问它们(据我所知)因为我必须假设明天他们将拥有完全不同的 ID
这是我尝试解析的一些代码
public ArrayList<Quake> decodeMessage(String message) {
try {
Log.d(TAG, "Parsing: " + message);
JSONArray jArray;
jArray = new JSONArray(message);
for (int i=0;i<jArray.length();i++){
JSONObject jObject;
JSONArray jFeaturesArr;
JSONObject jPropertiesObj;
JSONObject jGeometryObj;
JSONArray jCoordinatesArr;
jObject = jArray.getJSONObject(i);
jFeaturesArr = jObject.getJSONArray(Quake.FEATURES);
jPropertiesObj=jFeaturesArr.getJSONObject(1);
jGeometryObj = jFeaturesArr.getJSONObject(2);
jCoordinatesArr = jGeometryObj.getJSONArray(Quake.COORDINATES);
quakes.add(new
Quake(jPropertiesObj.getLong(Quake.DATE),
jPropertiesObj.getString(Quake.PLACE),
jCoordinatesArr.getDouble(1),
jCoordinatesArr.getDouble(0),
jPropertiesObj.getDouble(Quake.MAGNITUDE),
jPropertiesObj.getString(Quake.DETAIL)));
}
} catch (Exception e) {
Log.e(TAG, "decodeMessage: exception during parsing");
e.printStackTrace();
return null;
}
return quakes;
}
之后我得到一个错误
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
等(仅指向我的解码方法和我声明 JSONArray 的行)
我的地震class
public class Quake {
private long id;
private Date date;
private String location;
private double longitude;
private double magnitude;
private double latitude;
private String detail;
private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
public Quake()
{
}
public Quake(Long date, String location, double latitude, double longitude, double magnitude, String detail) {
this.date = new Date(date);
this.location = location;
this.latitude = latitude;
this.longitude = longitude;
this.magnitude = magnitude;
this.detail = detail;
}
public String getExtraText()
{
String output = "An Earthquake occurred at the time " + sdf.format(date)+
"with a magnitude of "+ magnitude + " and longitude and latitude of "
+longitude + " and " + latitude + " respectively. Notably the location was
"+location+ ".";
return output;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getMagnitude() {
return magnitude;
}
public void setMagnitude(double magnitude) {
this.magnitude = magnitude;
}
@Override
public String toString() {
String output= "";
output+= sdf.format(date);
output += " "+ magnitude + " " + location;
return output;
}
public static final String DATE = "time";
public static final String PLACE = "place";
public static final String MAGNITUDE = "mag";
public static final String DETAIL = "detail";
public static final String LONGITUDE = "longitude";
public static final String LATITUDE = "latitude";
public static final String GEOMETRY = "geometry";
public static final String FEATURES = "features";
public static final String PROPERTIES = "properties";
public static final String COORDINATES = "coordinates";
}
请注意,我仍在从之前使用数据库的任务中转移过来(我现在不需要,但是你)
JSONObject jsonObject;
try {
jsonObject = new JSONObject(message);
JSONArray jsonArray = jsonObject.getJSONArray(Quake.FEATURES);
//Continue get remain information
} catch (JSONException e) {
e.printStackTrace();
}
试试这个。因为完整的消息是一个 json 对象,你需要获取 json 数组,键为 "features"