0

私は、アプリケーション用に Web サイトから提供された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":"晴れのち曇り","降水量":0,"気温":20.65,"露点":10.56,"風速":9.92,"風方位":59,"雲量":0.34,"湿度":0.5,"気圧":1023.91,"可視性":10.75,"オゾン":356.06},"分単位":{"概要":"30分で小雨","アイコン":"雨"," 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,"降水量":0},{"時間":1370613240,"降水量":0},{"時間":1370613300,"降水量":0},{"時間":1370613360,"降水量":0 },{"時間":1370613420,"降水量":0},{"時間":1370613480,"降水量":0},{"時間":1370613540,"降水量":0},{"時間":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" :"雨"},{"時間":1370614200,"降水量":0.223,"降水量エラー":0.088,"降水確率":0.1,"降水量":"雨"},{"時間":1370614260,"降水量" :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"}, {"時間":1370614860,"降水量":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

すべての回答に感謝します。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));
                }
            }
        }
于 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 に答える