当前位置: 首页>>代码示例>>Java>>正文


Java Utils.openUrlConnection方法代码示例

本文整理汇总了Java中com.google.android.apps.dashclock.Utils.openUrlConnection方法的典型用法代码示例。如果您正苦于以下问题:Java Utils.openUrlConnection方法的具体用法?Java Utils.openUrlConnection怎么用?Java Utils.openUrlConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.android.apps.dashclock.Utils的用法示例。


在下文中一共展示了Utils.openUrlConnection方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getWeatherDataForLocation

import com.google.android.apps.dashclock.Utils; //导入方法依赖的package包/类
private static WeatherData getWeatherDataForLocation(LocationInfo li) throws IOException {
    HttpURLConnection connection = Utils.openUrlConnection(buildWeatherQueryUrl(li.woeid));

    try {
        XmlPullParser xpp = sXmlPullParserFactory.newPullParser();
        xpp.setInput(new InputStreamReader(connection.getInputStream()));

        WeatherData data = new WeatherData();
        boolean hasTodayForecast = false;
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG
                    && "condition".equals(xpp.getName())) {
                for (int i = xpp.getAttributeCount() - 1; i >= 0; i--) {
                    if ("temp".equals(xpp.getAttributeName(i))) {
                        data.temperature = Integer.parseInt(xpp.getAttributeValue(i));
                    } else if ("code".equals(xpp.getAttributeName(i))) {
                        data.conditionCode = Integer.parseInt(xpp.getAttributeValue(i));
                    } else if ("text".equals(xpp.getAttributeName(i))) {
                        data.conditionText = xpp.getAttributeValue(i);
                    }
                }
            } else if (eventType == XmlPullParser.START_TAG
                    && "forecast".equals(xpp.getName())
                    && !hasTodayForecast) {
                // TODO: verify this is the forecast for today (this currently assumes the
                // first forecast is today's forecast)
                hasTodayForecast = true;
                for (int i = xpp.getAttributeCount() - 1; i >= 0; i--) {
                    if ("code".equals(xpp.getAttributeName(i))) {
                        data.todayForecastConditionCode
                                = Integer.parseInt(xpp.getAttributeValue(i));
                    } else if ("text".equals(xpp.getAttributeName(i))) {
                        data.forecastText = xpp.getAttributeValue(i);
                    }
                }
            } else if (eventType == XmlPullParser.START_TAG
                    && "location".equals(xpp.getName())) {
                String cityOrVillage = "--";
                String region = "--";
                for (int i = xpp.getAttributeCount() - 1; i >= 0; i--) {
                    if ("city".equals(xpp.getAttributeName(i))) {
                        cityOrVillage = xpp.getAttributeValue(i);
                    } else if ("region".equals(xpp.getAttributeName(i))) {
                        region = xpp.getAttributeValue(i);
                    }
                }
                if (!TextUtils.isEmpty(li.town) && !li.town.equals(cityOrVillage)) {
                    data.location = cityOrVillage + ", " + li.town + ", " + region;
                } else {
                    data.location = cityOrVillage + ", " + region;
                }
            }
            eventType = xpp.next();
        }

        return data;

    } catch (XmlPullParserException e) {
        throw new IOException("Error parsing weather feed XML.", e);
    } finally {
        connection.disconnect();
    }
}
 
开发者ID:JesusM,项目名称:DashClock,代码行数:65,代码来源:WeatherExtension.java

示例2: getLocationInfo

import com.google.android.apps.dashclock.Utils; //导入方法依赖的package包/类
private static LocationInfo getLocationInfo(Location location)
        throws IOException, InvalidLocationException {
    LocationInfo li = new LocationInfo();
    HttpURLConnection connection = Utils.openUrlConnection(buildPlaceSearchUrl(location));
    try {
        XmlPullParser xpp = sXmlPullParserFactory.newPullParser();
        xpp.setInput(new InputStreamReader(connection.getInputStream()));

        boolean inWoe = false;
        boolean inTown = false;
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG && "woeid".equals(xpp.getName())) {
                inWoe = true;
            } else if (eventType == XmlPullParser.TEXT && inWoe) {
                li.woeid = xpp.getText();
            }

            if (eventType == XmlPullParser.START_TAG && xpp.getName().startsWith("locality")) {
                for (int i = xpp.getAttributeCount() - 1; i >= 0; i--) {
                    if ("type".equals(xpp.getAttributeName(i))
                            && "Town".equals(xpp.getAttributeValue(i))) {
                        inTown = true;
                    }
                }
            } else if (eventType == XmlPullParser.TEXT && inTown) {
                li.town = xpp.getText();
            }

            if (eventType == XmlPullParser.END_TAG) {
                inWoe = false;
                inTown = false;
            }

            eventType = xpp.next();
        }

        if (!TextUtils.isEmpty(li.woeid)) {
            return li;
        }

        throw new InvalidLocationException();

    } catch (XmlPullParserException e) {
        throw new IOException("Error parsing location XML response.", e);
    } finally {
        connection.disconnect();
    }
}
 
开发者ID:JesusM,项目名称:DashClock,代码行数:50,代码来源:WeatherExtension.java


注:本文中的com.google.android.apps.dashclock.Utils.openUrlConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。