url:http://www.scnewair.cn:3389/publish/getAllCity24HRealTimeAQICJava
把上述url的数据保存在TXT文件中,Java代码应该怎么写。
public class test {
public static void main(String[] args) throws IOException {
String basePath = "D:\\myprogram";
File newFile = new File(basePath + "\\" + System.currentTimeMillis());
if(!newFile.exists()) {
newFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(newFile);
out.write("http://www.scnewair.cn:3389/publish/getAllCity24HRealTimeAQICJava".getBytes());
out.flush();
out.close();
}
}
使用HttpClient
http://www.cnblogs.com/QQParadise/articles/5020215.html
分析了一下网站,它的数据是从http://www.scnewair.cn:3389/publish/getAllCity24HRealTimeAQIC拿得,返回的是一个json数组,然后你可以对应的解析处理了
try {
JSONArray objs = new JSONArray(response.body().string());
JSONObject data = objs.getJSONObject(0);
JSONArray citys = data.getJSONArray("data");
JSONObject city = citys.getJSONObject(0);
JSONObject columns = city.getJSONObject("columns");
String pm25 = columns.getString("PM2_5");
Log.i("okhttp3", "get pm 2.5 " + pm25);
}catch (JSONException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
你去网上搜索下网络爬虫。这个是静态的爬虫就是获取html页面的值,如果是动态的就需要模拟调用后台方法,之后返回内容。