所以在我正在考虑的课程中,我被要求使用Android Volley访问https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson“>这个并解析它们一个应用程序,所以我可以将它们粘贴在textview中。
老实说,我刚刚学习JSON和Volley,所以很可能我无法找到答案的原因是因为它非常明显我只是在思考,或者说是什么,但是我无法弄清楚如何采取这些对象并将它们粘贴在Java Arraylist中。它们没有被归类为JSONArray,所以我的代码在我尝试时失败了,并且它们在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)
etc(只是指向我的解码方法和我声明JSONArray的行)
我的Quake课程
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数组