请求的地址为:
page_num 需要每次+1 来获取参数
考虑到获取的速度问题,决定每个请求都创建一个线程放入线程池,代码如下
//
package threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class test {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(3);
int i = 1;
while(i<=4){
//4次http请求
pool.execute(new loadData(i));
i++;
}
}
}
//抓取数据的类,实现runable
package threadpool;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class loadData implements Runnable {
StringBuilder temp = new StringBuilder();
int pagenumber;
public loadData(int i){
this.pagenumber = i;
}
@Override
public void run() {
System.out.println(pagenumber);
try {
String address = "http://api.map.baidu.com/place/v2/search? &q=%E9%A5%AD%E5%BA%97®ion=%E9%87%8D%E5%BA%86&output=json&ak=10f2c8e6b9af4244be2453001b9dfe76&page_size=20&page_num="+pagenumber;
URL url=new URL(address);
HttpURLConnection Connection= (HttpURLConnection) url.openConnection();
InputStream is = Connection.getInputStream();
int len = 0; //每次读取的实际字节数
byte[] temp = new byte[1024]; //1024每次读取的字符的长度
StringBuilder sb = new StringBuilder();
//new String 的参数byte,offset,len
while((len=is.read(temp))!=-1){
sb.append(new String(temp,0,len)); //最后一次的长度不一定为1024
}
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}