我在使用此 JSON 对象开发的应用程序时遇到了一些问题:http://api.worldweatheronline.com/premium/v1/marine.ashx?q=-34.8799074,174.7565664&key=3477e975c4d9a8a0eaac2b2c818f4&format=json&tide=yes
有时它工作得很好,从 JSON 对象中获取数据没有问题,有时我得到这种错误:JSONException: Index 5 out of range [0..5]。
12-04 11:12:30.328 1916-1931/android_kaizen.com.drawerskippermate E/FetchWeatherTask﹕ Index 5 out of range [0..5)
org.json.JSONException: Index 5 out of range [0..5)
at org.json.JSONArray.get(JSONArray.java:263)
at org.json.JSONArray.getJSONObject(JSONArray.java:480)
at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.getWeatherDataFromJson(MainActivity.java:579)
at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.doInBackground(MainActivity.java:754)
at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.doInBackground(MainActivity.java:318)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:864)
甚至没有更改一行代码!! 奇怪的是在Web Browser中可以看到JSON Object。
有没有人经历过类似的情况,可以解释为什么会这样?
谢谢!
编辑:这是我的代码:
private DayForecast[] getWeatherDataFromJson(String forecastJsonStr) throws JSONException {
final String JSON_DATA = "data";
final String JSON_WEATHER = "weather";
final String JSON_MAX = "maxtempC";
final String JSON_MIN = "mintempC";
final String JSON_DATE = "date";
final String JSON_ARRAY_HOURLY = "hourly";
final String JSON_ARRAY_ASTRONOMY = "astronomy";
final String JSON_ARRAY_TIDES = "tides";
final int JSON_OBJECT_MORNING = 3;
final int JSON_OBJECT_MIDDAY = 4;
final int JSON_OBJECT_AFTERNOON = 5;
final String JSON_DESCRIPTION = "weatherDesc";
// WeatherCode and description at midday
final String JSON_WEATHER_CODE = "weatherCode";
final String JSON_WEATHER_DESCRIPTION = "weatherDesc";
final String JSON_TEMP = "tempC";
final String JSON_PRECIPITATION = "precipMM";
final String JSON_CLOUD_COVER = "cloudcover";
final String JSON_VISIBILITY = "visibility";
final String JSON_SUNRISE = "sunrise";
final String JSON_SUNSET = "sunset";
final String JSON_PRESSURE = "pressure";
final String JSON_WIND_DIRECTION = "winddir16Point";
final String JSON_WIND_SPEED = "windspeedKmph";
final String JSON_WIND_GUST = "WindGustKmph";
final String JSON_WATER_TEMP = "waterTemp_C";
final String JSON_SWELL_DIRECTION = "swellDir16Point";
final String JSON_SWELL_HEIGHT = "swellHeight_m";
final String JSON_TIDE = "tideTime";
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONObject(JSON_DATA).getJSONArray(JSON_WEATHER);
int numDays = weatherArray.length();
mDayForecastArray = new DayForecast[numDays];
for (int i = 0; i < weatherArray.length(); i++) {
String day;
String description;
String highAndLow;
JSONObject dayForecast = weatherArray.getJSONObject(i);
String dateTime = dayForecast.getString(JSON_DATE);
day = getReadableDateString(dateTime);
// Get the right JsonObject for different time of the day
JSONObject morningObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_MORNING);
JSONObject midDayObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_MIDDAY);
JSONObject afternoonObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_AFTERNOON);
/**
* Common Conditions.
*/
// Parse the two midday values from Json to variables
/**
* Morning Conditions.
*/
description = midDayObject.getJSONArray(JSON_DESCRIPTION).getJSONObject(0).getString("value");
int high = dayForecast.getInt(JSON_MAX);
int low = dayForecast.getInt(JSON_MIN);
highAndLow = formatHighLows(high, low);
DayForecast dayForecastObject = new DayForecast();
dayForecastObject.setmDate(day);
dayForecastObject.setmDescription(description);
dayForecastObject.setmWeatherCode(afternoonObject.getInt(JSON_WEATHER_CODE));
dayForecastObject.setmWeatherDescription(afternoonObject.getJSONArray(JSON_DESCRIPTION).getJSONObject(0).getString("value"));
dayForecastObject.setmTempMax(convertCelsiusToFahrenheit(dayForecast.getInt(JSON_MAX)));
dayForecastObject.setmTempMin(dayForecast.getInt(JSON_MIN));
/**
* Morning Conditions.
*/
dayForecastObject.setmPrecipitationAM(convertmillimetersToInches(morningObject.getInt(JSON_PRECIPITATION)));
dayForecastObject.setmCloudCoverAM(morningObject.getInt(JSON_CLOUD_COVER));
dayForecastObject.setmVisibilityAM(convertKilometersToMiles(morningObject.getInt(JSON_VISIBILITY)));
dayForecastObject.setmSunrise(dayForecast.getJSONArray(JSON_ARRAY_ASTRONOMY).getJSONObject(0).getString(JSON_SUNRISE));
dayForecastObject.setmPressureAM(morningObject.getInt(JSON_PRESSURE));
dayForecastObject.setmWindSpeedAM(convertKilometersToMiles(morningObject.getInt(JSON_WIND_SPEED)));
dayForecastObject.setmWindGustAM(convertKilometersToMiles(morningObject.getInt(JSON_WIND_GUST)));
dayForecastObject.setmWindDirectionAM(morningObject.getString(JSON_WIND_DIRECTION));
dayForecastObject.setmWaterTempAM(convertCelsiusToFahrenheit(morningObject.getInt(JSON_WATER_TEMP)));
dayForecastObject.setmSwellHeightAM(convertMetersToFeet(Float.parseFloat(morningObject.getString(JSON_SWELL_HEIGHT))));
dayForecastObject.setmSwellDirectionAM(morningObject.getString(JSON_SWELL_DIRECTION));
dayForecastObject.setmTide1AM(getReadableTide(dayForecast, 0));
dayForecastObject.setmTide2AM(getReadableTide(dayForecast, 1));
/**
* Afternoon Conditions.
*/
dayForecastObject.setmPrecipitationPM(convertmillimetersToInches(afternoonObject.getInt(JSON_PRECIPITATION)));
dayForecastObject.setmCloudCoverPM(afternoonObject.getInt(JSON_CLOUD_COVER));
dayForecastObject.setmVisibilityPM(convertKilometersToMiles(afternoonObject.getInt(JSON_VISIBILITY)));
dayForecastObject.setmSunset(dayForecast.getJSONArray(JSON_ARRAY_ASTRONOMY).getJSONObject(0).getString(JSON_SUNSET));
dayForecastObject.setmPressurePM(afternoonObject.getInt(JSON_PRESSURE));
dayForecastObject.setmWindSpeedPM(convertKilometersToMiles(afternoonObject.getInt(JSON_WIND_SPEED)));
dayForecastObject.setmWindGustPM(convertKilometersToMiles(afternoonObject.getInt(JSON_WIND_GUST)));
dayForecastObject.setmWindDirectionPM(afternoonObject.getString(JSON_WIND_DIRECTION));
dayForecastObject.setmWaterTempPM(convertCelsiusToFahrenheit(afternoonObject.getInt(JSON_WATER_TEMP)));
dayForecastObject.setmSwellHeightPM(convertMetersToFeet(Float.parseFloat(afternoonObject.getString(JSON_SWELL_HEIGHT))));
dayForecastObject.setmSwellDirectionPM(afternoonObject.getString(JSON_SWELL_DIRECTION));
dayForecastObject.setmTide1PM(getReadableTide(dayForecast, 2));
dayForecastObject.setmTide2PM(getReadableTide(dayForecast, 3));
mDayForecastArray[i] = dayForecastObject;
}
for (DayForecast s : mDayForecastArray) {
Log.v(LOG_TAG, "Forecast entry: " + s.getmDescription() + " / " + String.valueOf(s.getmWeatherCode()));
}
return mDayForecastArray;
}
最佳答案
我很确定您有类似以下问题: JSONArray Exception : Index 50 out of range [0..50). Is there any limit on JsonArray
.json 响应中的对象数量每天都会变化,您必须定义边界(results.length()):
看这个例子:
JSONArray results = jsonResponse.getJSONArray("results");
final int numberOfItemsInResp = results.length();
for (int i = 0; i < numberOfItemsInResp; i++){
JSONObject perResult = results.getJSONObject(i);
}
关于android - JSONException:索引 5 超出范围 [0..5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282673/
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天
我发现自己需要这个。假设cart是一个包含用户列表的模型。defindex_of_itemcart.users.each_with_indexdo|u,i|ifu==current_userreturniendend获取此类关联索引的更简单方法是什么? 最佳答案 indexArray上的方法与您的index_of_item方法相同,例如cart.users.index(current_user)返回数组中第一个对象的索引==给obj。如果未找到匹配项,则返回nil。 关于ruby-on-
因此,当我遵循MichaelHartl的RubyonRails教程时,我注意到在用户表中,我们为:email属性添加了一个唯一索引,以提高find的效率方法,因此它不会逐行搜索。到目前为止,我们一直在根据情况使用find_by_email和find_by_id进行搜索。然而,我们从未为:id属性设置索引。:id是否自动索引,因为它在默认情况下是唯一的并且本质上是顺序的?或者情况并非如此,我应该为:id搜索添加索引吗? 最佳答案 大多数数据库(包括sqlite,这是RoR中的默认数据库)会自动索引主键,对于RailsMigration
我想检查my_number是否在某个范围内,包括较高的值。在IF语句中我会简单地使用“x>100&&x但是我应该在Ruby案例中做什么(开关)?使用:casemy_numberwhenmy_number不起作用。备注:标准范围不包括my_number恰好为500的情况,并且我不想添加第二个“when”,因为我必须编写双重内容casemy_number#between100and500when100..500puts"Correct,dosomething"when500puts"Correct,dosomethingagain"end 最佳答案