我一直在尝试使用其网站提供的Forecast.io API和JAR用于我的应用程序。但是,在调用Web API时,似乎该站点返回的数据没有完全下载。
我尝试它打印数据,但似乎不是所有信息。
我正在使用此代码:
HttpClient client = new DefaultHttpClient();
URI website = new URI(requestURL);
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
scanner = new BufferedReader(new inputStreamReader(response.getEntity()
.getContent()));
String availalbe;
while ((availalbe = scanner.readLine()) != null) {
res += availalbe;
}
打印出的数据(在预期的途中停止):
{“纬度”:51.7589177,“经度”:-0.2342903,“时区”:“欧洲/伦敦”,“偏移量”:1,“当前”:{“时间”:1370612854,“摘要”:“部分多云”, “ icon”:“部分多云的日子”,“ precipIntensity”:0:“温度”:20.65,“ dewPoint”:10.56,“ windSpeed”:9.92,“ windBearing”:59,“ cloudCover”:0.34,“湿度“:0.5,”压力“:1023.91,”可见度“:10.75,”臭氧“:356.06},”分钟“:{”摘要“:” 30分钟内有小雨“,” icon“:”雨“,” data“:[{” time“:1370612820,” precipIntensity“:0},{” time“:1370612880,” precipIntensity“:0},{” time“:1370612940,” precipIntensity“:0},{” time“ :1370613000,“ precipIntensity”:0},{“ time”:1370613060,“ precipIntensity”:0},{“ time”:1370613120,“ precipIntensity”:0},{“ time”:1370613180,“ precipIntensity”:0 },{“ time”:1370613240,“ precipIntensity”:0},{“ time”:1370613300,“ precipIntensity”:0},{“ time”:1370613360,“ precipIntensity”:0},{“ time”:1370613420 ,“ precipIntensity”:0},{“ time”:1370613480,“ precipIntensity”:0},{“ time”:1370613540,“ precipIntensity”:0},{“ time”:1370613600,“ precipIntensity”:0}, {“ time”:1370613660,“ precip强度“:0.107,” precipIntensityError“:0.055,” precipProbability“:0.01,” precipType“:” rain“},{” time“:1370613720,” precipIntensity“:0.111,” precipIntensityError“:0.057,” precipProbability“:0.01 ,“ precipType”:“ rain”},{“ time”:1370613780,“ precipIntensity”:0.132,“ precipIntensityError”:0.065,“ precipProbability”:0.01,“ precipType”:“ rain”},{“ time”:1370613840 ,“ precipIntensity”:0.137,“ precipIntensityError”:0.062,“ precipProbability”:0.03,“ precipType”:“ rain”},{“ time”:1370613900,“ precipIntensity”:0.142,“ precipIntensityError”:0.065,“ precipProbability” :0.03,“ precipType”:“ rain”},{“ time”:1370613960,“ precipIntensity”:0.161,“ precipIntensityError”:0.072,“ precipProbability”:0.04,“ precipType”:“ rain”},{“ time” :1370614020,“ precipIntensity”:0.174,“ precipIntensityError”:0.074,“ precipProbability”:0.04,“ precipType”:“ rain”},{“ time”:1370614080,“ precipIntensity”:0.187,“ precipIntensityError”:0.077,“ precipProbability“:0.08,” precipType“:” rain“},{” time“:1370614140,” precipIntensity“:0.207,” precipIntensityError“:0.084,” precipPro bability“:0.09,” precipType“:” rain“},{” time“:1370614200,” precipIntensity“:0.223,” precipIntensityError“:0.088,” precipProbability“:0.1,” precipType“:” rain“},{”时间”:1370614260,“ precipIntensity”:0.224,“ precipIntensityError”:0.094,“ precipProbability”:0.15,“ precipType”:“ rain”},{“ time”:1370614320,“ precipIntensity”:0.243,“ precipIntensityError”:0.102 ,“ precipProbability”:0.16,“ precipType”:“ rain”},{“ time”:1370614380,“ precipIntensity”:0.259,“ precipIntensityError”:0.108,“ precipProbability”:0.17,“ precipType”:“ rain”}, {“ time”:1370614440,“ precipIntensity”:0.262,“ precipIntensityError”:0.108,“ precipProbability”:0.24,“ precipType”:“ rain”},{“ time”:1370614500,“ precipIntensity”:0.28,“ precipIntensityError” :0.115,“ precipProbability”:0.25,“ precipType”:“ rain”},{“时间”:1370614560,“ precipIntensity”:0.3,“ precipIntensityError”:0.12,“ precipProbability”:0.25,“ precipType”:“ rain” },{“ time”:1370614620,“ precipIntensity”:0.322,“ precipIntensityError”:0.125,“ precipProbability”:0.26,“ precipType”:“ rain”},{“ time”:1370614680,“ preci pIntensity“:0.33,” precipIntensityError“:0.125,” precipProbability“:0.33,” precipType“:” rain“},{” time“:1370614740,” precipIntensity“:0.352,” precipIntensityError“:0.131,” precipProbability“:0.34 ,“ precipType”:“ rain”},{“ time”:1370614800,“ precipIntensity”:0.375,“ precipIntensityError”:0.136,“ precipProbability”:0.34,“ precipType”:“ rain”},{“ time”:1370614860 ,“ precipIntensity”:0.38,“ precipIntensityError”:0.14,“ precipProbability”:0.42,“ precipType”:“ rain”},{“ time”:1370614920,“ precipIntensity”:0.402,“ precipIntensityError”:0.147,“ precipProbability” :0.42,“ precipType”:“ rain”},{“ time”:1370614980,“ precipIntensity”:0.425,“ precipIntensityError”:0.154,“ precipProbability”:0.42,“ precipType”:“ rain”},{“ time” :1370615040,“ precipIntensity”:0.432,“ precipIntensityError”:0.157,“ precipProbability”:0.5,“ precipType”:“ rain”},{“ time”:1370615100,“ precipIntensity”:0.454,“ precipIntensityError”:0.164,“ precipProbability“:0.5,” precipType“:” rain“},{” time“:1370615160,” precipIntensity“:0.477,” precipIntensityError“:0.168,” precipProbabi lity“:0.5,” precipType“:” rain“},{” time“:1370615220,” precipIntensit
调用Forecast Api测试类的方法
public void weatherLike()
{
StrictMode.enableDefaults();
MyLocation myLocation = new MyLocation();
myLocation.getLocation(MyService.this, new LocationResult() {
ForecastIO fio = null;
@Override
public void gotLocation(Location location) {
try {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
fio= new ForecastIO("[API_KEY]");
} catch (Exception e) {
speakOut(user + ", I am not able to locate you");
e.printStackTrace();
}finally
{
System.out.println("Latitude: "+fio.getLatitude());
System.out.println("Longitude: "+fio.getLongitude());
System.out.println("Timezone: "+fio.getTimezone());
System.out.println("Offset: "+fio.offsetValue());
System.out.println("\n");
}
}
});
}
最佳答案
感谢所有答案。我发现My LogCat正在处理长消息,这也难怪我从未显示完整答复。
万一有人遇到同样的问题。
使用此代码将字符串回复拆分为多个部分
if (sb.length() > 4000) {
Log.v("length", "sb.length = " + sb.length());
int chunkCount = sb.length() / 4000; // integer division
for (int i = 0; i <= chunkCount; i++) {
int max = 4000 * (i + 1);
if (max >= sb.length()) {
Log.v("1st", "chunk " + i + " of " + chunkCount + ":" + sb.substring(4000 * i));
} else {
Log.v("2nd", "chunk " + i + " of " + chunkCount + ":" + sb.substring(4000 * i, max));
}
}
}