请问如何用java获取response中的内容我访问的是http://www.weather.com.cn/weather/101020100.shtml
这个截图是在google浏览器中F12-》network->XHR中获取到的。请问在java中怎么才能够拿到reponse中的内容。
java获取响应数据,急问。
答案:2 悬赏:30
解决时间 2021-02-04 01:11
- 提问者网友:陪我到最后
- 2021-02-03 19:30
最佳答案
- 二级知识专家网友:年轻没有失败
- 2021-02-03 20:31
这个获取天气的接口我做过,在微信上用的
private String getWeatherData(String urlStr){
String result = "";
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
//对应的字符编码转换
Reader reader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
reader.close();
connection.disconnect();
String jsonString = sb.toString();
JSONArray json = JSONArray.fromObject("["+jsonString+"]");
JSONObject jsobj = json.getJSONObject(0);
JSONArray jsonWeatherinfo = JSONArray.fromObject("["+jsobj.get("weatherinfo")+"]");
for (int i = 0; i < jsonWeatherinfo.size(); i++) {
JSONObject map = JSONObject.fromObject(jsonWeatherinfo.get(i));
result += "今天是"+map.get("date_y")+" "+map.get("week");
result += "\n["+map.get("city")+"]";
result += "今天天气:"+map.get("weather1")+",气温"+map.get("temp1")+","+map.getString("wind1");
result +="\n穿衣建议: ["+map.get("index_d")+"]\n";
result += "明天天气:"+map.get("weather2")+",气温"+map.get("temp2")+","+map.getString("wind2");
result +="\n穿衣建议: ["+map.get("index48_d")+"]\n";
result +="感谢您的使用!";
// System.out.println("明天天气:"+map.get("weather2")+" 温度:"+map.get("temp2"));
// System.out.println("后天天气:"+map.get("weather3")+" 温度:"+map.get("temp3"));
// System.out.println("大后天天气:"+map.get("weather4")+" 温度:"+map.get("temp4"));
//
// System.out.println("明日天气指南:["+map.get("index48")+"] "+map.get("index48_d"));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private String getWeatherData(String urlStr){
String result = "";
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
//对应的字符编码转换
Reader reader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
reader.close();
connection.disconnect();
String jsonString = sb.toString();
JSONArray json = JSONArray.fromObject("["+jsonString+"]");
JSONObject jsobj = json.getJSONObject(0);
JSONArray jsonWeatherinfo = JSONArray.fromObject("["+jsobj.get("weatherinfo")+"]");
for (int i = 0; i < jsonWeatherinfo.size(); i++) {
JSONObject map = JSONObject.fromObject(jsonWeatherinfo.get(i));
result += "今天是"+map.get("date_y")+" "+map.get("week");
result += "\n["+map.get("city")+"]";
result += "今天天气:"+map.get("weather1")+",气温"+map.get("temp1")+","+map.getString("wind1");
result +="\n穿衣建议: ["+map.get("index_d")+"]\n";
result += "明天天气:"+map.get("weather2")+",气温"+map.get("temp2")+","+map.getString("wind2");
result +="\n穿衣建议: ["+map.get("index48_d")+"]\n";
result +="感谢您的使用!";
// System.out.println("明天天气:"+map.get("weather2")+" 温度:"+map.get("temp2"));
// System.out.println("后天天气:"+map.get("weather3")+" 温度:"+map.get("temp3"));
// System.out.println("大后天天气:"+map.get("weather4")+" 温度:"+map.get("temp4"));
//
// System.out.println("明日天气指南:["+map.get("index48")+"] "+map.get("index48_d"));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
全部回答
- 1楼网友:时光不老我们不分离
- 2021-02-03 21:54
用sql就可以啊。
非要用java的话,从表里查出数据;针对日期做个for循环,在循环里把data累加。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |