import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import net.sf.json.JSONObject;
public class getWeatherJson {
private URL url;
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
buffer = new BufferedReader(new InputStreamReader(
conn.getInputStream(),"utf-8")); //此处不转码 会出现乱码情况
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void main(String []args){
JSONObject jsons = JSONObject.fromString(json.getString("weatherinfo"));
System.out.println(jsons.getString("SD"));
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import net.sf.json.JSONObject;
public class getWeatherJson {
private URL url;
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
buffer = new BufferedReader(new InputStreamReader(
conn.getInputStream(),"utf-8")); //此处不转码 会出现乱码情况
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void main(String []args){
System.out.println(new getWeatherJson().download("http://www.weather.com.cn/data/sk/101010100.html"));
//此处需要转成json 返回前端根据需求
JSONObject jsons = JSONObject.fromString(json.getString("weatherinfo"));
System.out.println(jsons.getString("SD"));
}
}