在JSON数据错误中找不到一个或多个字段

时间:2014-09-21 15:11:06

标签: android json android-asynctask

我正在为Android创建天气应用程序,我收到此错误。没有生成其他错误,只显示白屏,但应用程序没有崩溃。

  

JSON数据中找不到一个或多个字段

我正在使用此链接从openweathermap获取JSON数据(我已因个人原因删除了APPID)

  

http://api.openweathermap.org/data/2.5/weather?q=new%20york&units=metric&mode=json

我的问题是,默认情况下我将纽约作为默认位置通过,我只是在应用程序启动时遇到错误,但是一旦我更改了城市名称(我添加了手动更改城市名称的选项)在应用程序中)然后错误消失,它显示天气信息。该错误仅在第一次启动应用程序时出现,并且我作为参数传递的城市并不重要。

应用程序永远不会崩溃。

抱歉我的英文不好

这是我的代码

   public class FetchWeatherTask extends AsyncTask<String, Void, JSONObject>

    {

        private static final String OPEN_WEATHER_MAP_API = "http://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&mode=json";

        @Override
        protected JSONObject doInBackground(String... city) {

                try {
                   URL url = new URL(String.format(OPEN_WEATHER_MAP_API, URLEncoder.encode(city[0], "UTF-8")));

            HttpURLConnection connection =
                    (HttpURLConnection)url.openConnection() ;
            connection.setRequestMethod("GET");
            connection.connect();


            System.out.println("URL "+url);

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));

            StringBuffer json = new StringBuffer(1024);
            String tmp="";
            while((tmp=reader.readLine())!=null)
                json.append(tmp).append("\n");
            reader.close();

            JSONObject data = new JSONObject(json.toString());

            // This value will be 404 if the request was not successful
            if(data.getInt("cod") != 200){
                System.out.println("Error 200");
                return null;
            }
            else if(data.getInt("cod")!= 404)
            {
                System.out.println("Error 404");
                return null;
            }
            System.out.println("JSON DATA " +data);

            return data;
        }catch(Exception e){
            return null;
        }

        }

这是我打电话给上面的课程

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/weatherFont.ttf");
        updateWeatherData(new CityPreference(getActivity()).getCity().trim());
    }

    private void updateWeatherData(final String city){
            final FetchWeatherTask task = new FetchWeatherTask();
            try {
                final JSONObject json = task.execute(city).get();
                if (json == null) {
                    Toast.makeText(getActivity(),
                            getActivity().getString(R.string.place_not_found),
                            Toast.LENGTH_LONG).show();
                } else {

                    renderWeather(json);

                }
            }
            catch (Exception e )
            {
                e.printStackTrace();
            }


        }

错误

09-21 20:29:57.569  24486-24486/weather.samvid.com.sarcasticweather E/SimpleWeather﹕ One or more fields not found in the JSON data
09-21 20:29:57.619  24486-24486/weather.samvid.com.sarcasticweather I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
09-21 20:29:57.649  24486-24486/weather.samvid.com.sarcasticweather D/OpenGLRenderer﹕ Enabling debug mode 0
09-21 20:29:57.689  24486-24486/weather.samvid.com.sarcasticweather D/dalvikvm﹕ GC_FOR_ALLOC freed 357K, 4% free 9373K/9764K, paused 12ms, total 13ms

更新: 重新启动我的手机和Android工作室后,错误也发生了变化,这是新的错误

09-21 22:04:59.951    7483-7992/weather.samvid.com.sarcasticweather I/System.out﹕ URL http://api.openweathermap.org/data/2.5/weather?q=Ahmedabad+&units=metric&mode=json

09-21 22:05:00.993    7483-7992/weather.samvid.com.sarcasticweather I/System.out﹕ Error 404
09-21 22:05:01.023    7483-7483/weather.samvid.com.sarcasticweather I/Choreographer﹕ Skipped 63 frames!  The application may be doing too much work on its main thread.
09-21 22:06:06.122    7483-7483/weather.samvid.com.sarcasticweather W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
09-21 22:06:06.122    7483-7483/weather.samvid.com.sarcasticweather W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
09-21 22:06:06.122    7483-7483/weather.samvid.com.sarcasticweather W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
09-21 22:06:06.122    7483-7483/weather.samvid.com.sarcasticweather W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection

0 个答案:

没有答案