手机地图-httpclient+htmlparser+jdom展示yahoo天气预报

手机地图-httpclient+htmlparser+jdom展示yahoo天气预报
    在我要地图手机版中提供了查询当前城市未来三天内天气预报的功能。天气预报中提供了天气情况,空气质量,穿衣指数,洗车指数等信息。
    具体的实现思路是,通过httpclient发送查询请求到yahoo的天气预报接口,通过htmlparser解析返回结果,并且通过jdom包装成一个xml返回到j2me实现的手机客户端。继而展示!
    代码如下:
   
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            HttpClient client = new HttpClient(connectionManager);
            client.getHostConfiguration().setProxy("proxy******", *****);
            Credentials defaultcreds = new UsernamePasswordCredentials(
                    "XXXXXX@****.com", "XXXXXXXXX");
            client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
            
            
            outdoc = new Document();
            //增加根节点
            Element rootElement = new Element("handmap");
            rootElement.setAttribute("version", "1.0");
            
            HttpMethod method = new PostMethod(
                    "http://yahooweatherurl?city="
                            + URLEncoder.encode(this.cy, "utf-8"));
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            System.out.println(result);
            System.out.println("----------------------------");
            Parser parser;

            parser = new Parser(result);
            parser.setEncoding("GBK");
            // 寻找div的class是box2 p14的内容
            NodeFilter filter = new AndFilter(new TagNameFilter("div"),
                    new HasAttributeFilter("class", "box6"));
            NodeList nodeList = parser.parse(filter);
            parser.setInputHTML(result);

            NodeFilter contentfilter = new AndFilter(new TagNameFilter("div"),
                    new HasAttributeFilter("class", "box7"));
            NodeList contentNodeList = parser.parse(contentfilter);

            NodeIterator it = nodeList.elements();
            NodeIterator contentIt = contentNodeList.elements();
            Node node = null;
            Node contentNode = null;
            StringBuffer buffer=new StringBuffer(500);
            while (it.hasMoreNodes()) 
{
                node = it.nextNode();
                contentNode = contentIt.nextNode();
                String nodehtml = node.toHtml();
                String day = StringUtils.substringBetween(nodehtml,
                        "<span class=\"f18\">", "</p>");
                day = day.replaceAll("</span><br />", " ");
                day = day.replaceAll("  ", "");
                buffer.append(day).append("\n");
                //System.out.println(day);
                String weather = StringUtils.substringBetween(nodehtml,
                        "<span class=\"f14\"><strong>", "</strong>");
                weather = weather.replaceAll("    ", "");
                buffer.append(weather).append("\n");
                //System.out.println(weather);
                String content = contentNode.toPlainTextString().replaceAll(
                        "                    ", "");
                buffer.append(content).append("\n\n");
            
}
            rootElement.setText(buffer.toString());
            outdoc.setRootElement(rootElement);
        } catch (UnsupportedEncodingException e) 
{
            log.error(e.getLocalizedMessage());
        
}  catch (MalformedURLException e)  {
            log.error(e.getLocalizedMessage());
        
}  catch (IOException e)  {
            log.error(e);
        
}  catch (ParserException e)  {
            e.printStackTrace();
        
}

   当中需要说明的是,因为公司使用代理,所以在httpclient中我加入了代理设置:
   
            client.getHostConfiguration().setProxy("proxy", port);
            Credentials defaultcreds = new UsernamePasswordCredentials(
                    "[email protected]", "XXXXXXXXX");
            client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);        


你可能感兴趣的:(手机地图-httpclient+htmlparser+jdom展示yahoo天气预报)