Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用安卓截击从站点获取一组对象?不是数组_Java_Android_Json_Android Volley_Geojson - Fatal编程技术网

Java 使用安卓截击从站点获取一组对象?不是数组

Java 使用安卓截击从站点获取一组对象?不是数组,java,android,json,android-volley,geojson,Java,Android,Json,Android Volley,Geojson,因此,在我目前正在学习的课程中,我被要求使用Android Volley来访问https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson“>然后在应用程序中解析它们,这样我就可以将它们粘贴到文本视图中 老实说,我刚刚学习了JSON和Volley,所以我找不到答案的原因很可能是因为我显然是想得太多了,但我不太明白如何将这些对象放入Java Arraylist中。它们没有归类为JSONArray,所以我的代码在尝

因此,在我目前正在学习的课程中,我被要求使用Android Volley来访问https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson“>然后在应用程序中解析它们,这样我就可以将它们粘贴到文本视图中

老实说,我刚刚学习了JSON和Volley,所以我找不到答案的原因很可能是因为我显然是想得太多了,但我不太明白如何将这些对象放入Java Arraylist中。它们没有归类为JSONArray,所以我的代码在尝试时失败了,他们在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;

}
请注意,我仍然在从以前的作业转移过来,我在那里使用了一个数据库(我现在不需要数据库,但你需要)


试试这个。因为完整的消息是一个json对象,你需要获取带有关键“功能”的json数组。

请添加Quake POJO代码。添加的Quake类虽然有效,但它不会让我投票给你,因为我刚刚加入xD thanksah thanks没有意识到xD当然还有其他问题,但我想我可以解决,这毕竟是调试
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
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();
    }