1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
package jee.gwt.server;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import jee.gwt.client.YweatherService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class YweatherServiceImpl extends RemoteServiceServlet implements YweatherService
{
/**
*
*/
private static final long serialVersionUID = 1L;
private static DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
public YweatherServiceImpl()
{
}
public String getWeatherHtmlInfos(String zip)
{
return getWeatherHtml( getWeatherRssDocument(zip) );
}
private String getWeatherHtml(Document rss)
{
Element item = (Element)rss.getElementsByTagName("item").item(0);
Element desc = (Element)item.getElementsByTagName("description").item(0);
return desc.getFirstChild().getNodeValue();
}
private Document getWeatherRssDocument(String zip)
{
String url = getYahooRssUrl(zip);
// URLConnection conn = null;
// try {
// conn = new URL(url).openConnection();
// }
// catch (MalformedURLException e3)
// {
// e3.printStackTrace();
// }
// catch (IOException e3)
// {
// e3.printStackTrace();
// }
//MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(url);
try
{
int resultCode = client.executeMethod(get);
if (resultCode == 200)
{
InputStream in = get.getResponseBodyAsStream();
//in = conn.getInputStream();
DocumentBuilder builder = null;
try
{
builder = builderFactory.newDocumentBuilder();
try
{
return builder.parse(in);
}
catch (SAXException e1)
{
Window.alert( "Erreur d'analyse : " + e1.getMessage() );
}
}
catch (ParserConfigurationException e2)
{
Window.alert( "Erreur de Configuration du Parser : " + e2.getMessage() ); }
}
}
catch (IOException e)
{
Window.alert( "Probleme de communication HTTP" + e.getMessage() );
}
// finally
// {
// get.releaseConnection();
// }
return null;
}
private String getYahooRssUrl(String zip)
{
final String base= "http://xml.weather.yahoo.com/forecastrss?";
return base + "p=" + zip;
}
} |
Partager