简体   繁体   English

检索地震 JSON 结果时出现问题?

[英]Problem retrieving the earthquake JSON results?

Greeting!问候! After I removed hardcoded JSON data and moved to request data from the URL.在我删除了硬编码的JSON数据并移动到从 URL 请求数据之后。 I am having an exception error.我遇到异常错误。 The code is pretty much the same as the final official git but I am getting the errors.代码与最终的官方 git 几乎相同,但我收到了错误。

The code that I am extracting data from JSON is:我从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;
    }

The logcat shows: 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.

The json data from your url is not formatted correctly.来自您 url 的 json 数据格式不正确。 Compare what you are receiving from the url to the hardcoded data.将您从 url 接收到的内容与硬编码数据进行比较。

This method runs on the main UI thread after the background work has been completed.此方法在后台工作完成后在主 UI 线程上运行。 This method receives as input, the return value from the doInBackground() method.此方法接收doInBackground()方法的返回值作为输入。

First we clear out the adapter, to get rid of earthquake data from a previous query to USGS.首先,我们清除适配器,以清除先前向 USGS 查询的地震数据。

Then we update the adapter with the new list of earthquakes, which will trigger the ListView to re-populate its list items.然后我们用新的地震列表更新适配器,这将触发ListView重新填充其列表项。 But made mistake on populating data to adapter.但是在将数据填充到适配器时出错了。

        @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);
            }
        }

The real problem was onPostExecute method to populate data in mainthread after do in background method.真正的问题是onPostExecute方法在后台方法中执行后填充onPostExecute中的数据。

If you're taking the Udacity android course and encountering this error for the quakereport/DidUfeelIt app then change the URL and try with some other URL your problem will be solved.如果您正在参加 Udacity android 课程并在 quakereport/DidUfeelIt 应用程序中遇到此错误,请更改 URL 并尝试使用其他一些 URL,您的问题将得到解决。 Eg:- The URL provided by during the course was " http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6 "例如:- 课程期间提供的 URL 是“ http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6

Then I was getting the same error that is "problem parsing the JSON" So I tried different URL: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson然后我得到了同样的错误,即“解析 JSON 问题”所以我尝试了不同的 URL: https : //earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson

And it worked..!!它奏效了..!! Always try to get the latest URL's from the USGS website during the course.在课程期间始终尝试从 USGS 网站获取最新的 URL。

Change your request URL to USGS_REQUEST_URL=" https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=6&limit=10 "将您的请求 URL 更改为 USGS_REQUEST_URL=" https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=6&limit=10 "

The difference is in the use of https instead of http.不同之处在于使用 https 而不是 http。

According to the API doc of the USGS website this website now uses the HyperText Transfer Protocol Secure (https) https://earthquake.usgs.gov/fdsnws/event/1/根据美国地质调查局网站的 API 文档,该网站现在使用安全超文本传输​​协议 (https) https://earthquake.usgs.gov/fdsnws/event/1/

Use Online Json Validator.使用在线 Json 验证器。 http://jsonlint.com/ they will validate if the json has problems. http://jsonlint.com/他们将验证 json 是否有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM