0

我一直在尝试使用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;
}

打印出来的数据(它在预期中途停止):

{"latitude":51.7589177,"longitude":-0.2342903,"timezone":"Europe/London","offset":1,"currently":{"time":1370612854,"summary":"部分多云", "icon":"部分阴天","precipIntensity":0,"temperature":20.65,"dewPoint":10.56,"windSpeed":9.92,"windBearing":59,"cloudCover":0.34,"湿度":0.5,"pressure":1023.91,"visibility":10.75,"ozone":356.06},"minutely":{"summary":"30 分钟小雨","icon":"rain","数据":[{"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,"precipIntensity":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,"precipProbability":0.09,"precipType" :"rain"},{"time":1370614200,"precipIntensity":0.223,"precipIntensityError":0.088,"precipProbability":0.1,"precipType":"rain"},{"time":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"},{"time": 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,"precipIntensity":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" :"雨"},{"时间":1370615160,"precipIntensity":0.477,"precipIntensityError":0.168,"precipProbability":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");

            }
            }
        }); 
}
4

3 回答 3

2

感谢所有的答案。我发现我的 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));
                }
            }
        }
于 2013-06-07T18:20:01.933 回答
1

BufferedReader 使用“缓冲区”,因此您必须使用“刷新方法”(或关闭方法)参考链接:http ://docs.oracle.com/javase/tutorial/essential/io/buffers.html

于 2013-06-07T14:32:17.320 回答
0

试试这个

        public static String getResponseText(String stringUrl) throws IOException
{
    StringBuilder response  = new StringBuilder();

    System.out.println("webservice 1");
    URL url = new URL(stringUrl);
    HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
    if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
    {
        System.out.println("webservice 2");
        BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
        String strLine = null;
        while ((strLine = input.readLine()) != null)
        {
            response.append(strLine);
        }
        input.close();
    }
    return response.toString();
}
于 2013-06-07T14:10:52.570 回答