问候!我删除了硬编码的json数据并移动到从url请求数据。我有异常错误。代码与最终官方git几乎相同,但我收到了错误。
我从json中提取数据的代码是: private static final String USGS_REQUEST_URL = “http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=10”;
//
public static List<Earthquake> extractFeaturesfromJson(String earthquakeJSON) {
/*if the json is null then return earlu*/
if (TextUtils.isEmpty(earthquakeJSON)) {
return null;
}
// Create an empty ArrayList that we can start adding earthquakes to
List<Earthquake> earthquakes = new ArrayList<>();
// Try to parse the JsonResponseString. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
// create an object form jsonString
JSONObject root = new JSONObject(earthquakeJSON);
JSONArray features = root.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
// Get a single earthquake at position i within the list of earthquakes
JSONObject currentEarthquake = features.getJSONObject(i);
// For a given earthquake, extract the JSONObject associated with the
// key called "properties", which represents a list of all properties
// for that earthquake.
JSONObject properties = currentEarthquake.getJSONObject("properties");
double mag = properties.getDouble("mag");
String location = properties.getString("place");
long time = properties.getLong("time");
//extract the value of key url
String url = properties.getString("url");
//create new object with magnitude, location ane time and url from json response
Earthquake earthquake = new Earthquake(mag, location, time, url);
earthquakes.add(earthquake);
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
// Return the list of earthquakes
return earthquakes;
}
logcat显示:
09-26 14:49:23.628 2551-2584/com.example.android.quakereport E/com.example.android.quakereport.QueryUtils: Problem retrieving the earthquake JSON results.
答案 0 :(得分:0)
您网址中的json数据格式不正确。将您从网址收到的内容与硬编码数据进行比较。
答案 1 :(得分:0)
此方法在后台工作完成后在主UI线程上运行
完成。此方法接收来自doInBackground()
方法的返回值作为输入。
首先我们清除适配器,以摆脱以前的地震数据 查询USGS。
然后我们使用新的地震列表更新适配器,这将触发ListView
重新填充其列表项。 但是在将数据填充到适配器时犯了错误。
@Override
protected void onPostExecute(List<Earthquake> data) {
//clear the adapter of previdous earthquake data
mAdapter.clear();
if (data != null && data.isEmpty()) { //====?????shourld be !data.isEmpty())
mAdapter.addAll(data);
}
}
真正的问题是onPostExecute
方法在后台方法执行后填充mainthread中的数据。
答案 2 :(得分:0)
如果你正在使用Udacity android课程并遇到quakereport / DidUfeelIt应用程序的此错误,那么更改URL并尝试使用其他URL来解决您的问题。 例如: - 课程期间提供的URL是 &#34; http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6&#34;
然后我得到了同样的错误,即#34;解析JSON&#34; 所以我尝试了不同的URL: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson
它有效.. !! 在课程期间,始终尝试从USGS网站获取最新的URL。
答案 3 :(得分:0)
将您的请求网址更改为USGS_REQUEST_URL =“https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=6&limit=10”
区别在于使用https而不是http。
根据USGS网站的API文档,该网站现在使用安全超文本传输协议(https) https://earthquake.usgs.gov/fdsnws/event/1/
答案 4 :(得分:-1)
使用在线Json验证器。 http://jsonlint.com/ 他们将验证json是否有问题。