import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;public class WeatherManager 
{
public static void main(String[] args)
{
WeatherManager manager=new WeatherManager();
String weather_xml=manager.getWeather("Beijing");
System.out.println(weather_xml);
}
public String getWeather(String city_py)
{
String city_weather_url=googleWeatherUrl+"&weather="+city_py;
URL url=null;
URLConnection urlConn=null;
HttpURLConnection httpConn=null;
InputStream is = null;
try 
{
url=new URL(city_weather_url);
urlConn=url.openConnection();
httpConn=(HttpURLConnection)urlConn;
httpConn.setConnectTimeout(100*1000);
httpConn.setReadTimeout(100*1000);
httpConn.connect();
int ret_code=httpConn.getResponseCode();
if(ret_code==HttpURLConnection.HTTP_OK)
{
is=(InputStream)httpConn.getInputStream();
InputStreamReader reader=new InputStreamReader(is,"gbk");
BufferedReader buffReader=new BufferedReader(reader);
StringBuffer buffer=new StringBuffer();
String line=buffReader.readLine();
while(line!=null)
{
buffer.append(line);
line=buffReader.readLine();
}
String ret_xml=buffer.toString();
return ret_xml;
}

catch (MalformedURLException  e) 
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
} finally {
if(httpConn!=null) {
httpConn.disconnect();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
private static final String googleWeatherUrl=" http://www.google.com/ig/api?hl=zh-cn";
}