Java 检索地震JSON结果时出现问题?
问候!之后,我删除了硬编码的Java 检索地震JSON结果时出现问题?,java,android,json,Java,Android,Json,问候!之后,我删除了硬编码的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";
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;
}
url中的json数据格式不正确。将从url接收到的内容与硬编码数据进行比较。使用在线Json验证程序。
他们将验证json是否有问题。后台工作完成后,此方法在主UI线程上运行 完整的。此方法接收来自
doInBackground()
方法的返回值作为输入
首先,我们清除适配器,以从以前的
向美国地质调查局查询
然后我们用新的地震列表更新适配器,这将触发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);
}
}
@覆盖
受保护的void onPostExecute(列表数据){
//清除先前地震数据的适配器
mAdapter.clear();
如果(data!=null&&data.isEmpty()){/=???应该是!data.isEmpty())
mAdapter.addAll(数据);
}
}
真正的问题是在后台执行方法之后在主线程中填充数据的
onPostExecute
方法。如果您正在学习Udacity android课程,并且在QuakerReport/DidUfeelIt应用程序中遇到此错误,则更改URL并尝试使用其他URL,您的问题将得到解决。
示例:-课程期间提供的URL为
“”
然后我遇到了相同的错误,即“解析JSON时出现问题”
所以我尝试了不同的URL:
它成功了。。!!
在课程期间,始终尝试从USGS网站获取最新的URL。将您的请求URL更改为USGS_request_URL=“” 区别在于使用https而不是http 根据美国地质勘探局网站的API文件,该网站现在使用超文本传输协议安全(https)
请提供获取异常的日志请发布错误日志、正在解析的JSON数据以及用于解析JSON的代码。我发现您使用的
索引10超出了范围[0..10)
也许您使用for循环来获取对象?如果为真,您应该检查循环的索引。1-未收到JSON | 2-JSON与您的测试不同。url和硬编码的JSON数据相同。上面的JSON提取器代码中是否存在错误?您的日志显示:09-26 14:49:23.628 2551-2584/com.example.android.quakereport E/com.example.android.quakereport.QueryUtils:检索地震JSON结果时出现问题”-当然这与解析无关。
@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);
}
}