我试过你的代码,但没有用:
如果对你有帮助,我做了一些改变:
地震
import java.util.ArrayList;
public class Earthquake {
ArrayList<Property> properties= new ArrayList<Property>();
public Earthquake(long t , long m , String p){
Property pr = new Property(m ,p, t);
properties.add(pr);
}
}
属性
public class Property {
private long mMagnitude;
private String mLocation;
private long mDate;
public Property(long mMagnitude, String mLocation, long mDate) {
this.mMagnitude = mMagnitude;
this.mLocation = mLocation;
this.mDate = mDate;
}
}
主要
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
public class MainProp {
public static void main(String[] args) throws JSONException {
// TODO Auto-generated method stub
List<Earthquake> earthquakes = new ArrayList<Earthquake>();
List<Earthquake> fromjson = new ArrayList<Earthquake>();
JSONParser jsp = new JSONParser();
String url ="https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmagnitude=6";
JSONObject obj = jsp.makeHttpRequest(url, "GET");
JSONArray earthquakeArray = obj.getJSONArray("features");//baseJsonResponse.optJSONArray("features");
for (int i = 0; i < earthquakeArray.length(); i++) {
JSONObject currentEarthquake = earthquakeArray.optJSONObject(i);
JSONObject properties = currentEarthquake.optJSONObject("properties");
Iterator<?> keys = properties.keys();
long time =0;
String location ="";
long magnitude =0;
Earthquake earthquake = null;
while( keys.hasNext() ) {
String key = (String)keys.next();
if("time".equals(key))
time = properties.getLong("time");
if("place".equals(key))
location = properties.getString("place");
if("mag".equals(key))
magnitude = properties.getLong("mag");
if(!"".equals(location) && time !=0 && magnitude != 0){
earthquake = new Earthquake(time,magnitude,location);
earthquakes.add(earthquake);
}
}
Gson gson = new Gson();
String json = gson.toJson(earthquake);
System.out.println(json);
Earthquake fromjsonItem = gson.fromJson(json, Earthquake.class);
fromjson.add(fromjsonItem);
}
}
}
解析器 DefaultHttpClient 现已弃用:(
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
return jObj;
}
}
结果