环境准备:
1.JDK
2.eclipse
3.jsoup.jar
GET 类
package com.gaoyx.get;
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
//import java.io.*;
//import java.sql.Connection;
//import javax.swing.text.Document;
//import org.jsoup.Jsoup;
public class GetTest {
/**
* @param args
*/
public void testJsop(){
try {
Connection conn=Jsoup.connect("http://v.juhe.cn/weather/index").ignoreContentType(true); //接口地址
conn.data("format","2"); //参数
conn.data("cityname","上海"); //参数
conn.data("key","c4f0df34872ead401bcd338ea7497b11");
Document doc=conn.get();
System.out.println(doc);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GetTest gt=new GetTest();
gt.testJsop();
}
}
返回结果:
<html>
<head></head>
<body>
{"resultcode":"200","reason":"successed!","result":{"sk":{"temp":"14","wind_direction":"东风","wind_strength":"1级","humidity":"53%","time":"15:12"},"today":{"temperature":"8℃~14℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"北风微风","week":"星期四","city":"上海","date_y":"2016年12月01日","dressing_index":"较冷","dressing_advice":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","uv_index":"最弱","comfort_index":"","wash_index":"较适宜","travel_index":"较适宜","exercise_index":"较适宜","drying_index":""},"future":[{"temperature":"8℃~14℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"北风微风","week":"星期四","date":"20161201"},{"temperature":"9℃~14℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"东北风微风","week":"星期五","date":"20161202"},{"temperature":"11℃~16℃","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"wind":"东风微风","week":"星期六","date":"20161203"},{"temperature":"8℃~15℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"西风微风","week":"星期日","date":"20161204"},{"temperature":"6℃~15℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"西风微风","week":"星期一","date":"20161205"},{"temperature":"8℃~15℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"西风微风","week":"星期二","date":"20161206"},{"temperature":"9℃~14℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"东北风微风","week":"星期三","date":"20161207"}]},"error_code":0}
</body>
</html>
Post 类
把get方法改为post方法。