java获取响应数据,急问。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java获取响应数据,急问。相关的知识,希望对你有一定的参考价值。
请问如何用java获取response中的内容我访问的是http://www.weather.com.cn/weather/101020100.shtml
这个截图是在google浏览器中F12-》network->XHR中获取到的。请问在java中怎么才能够拿到reponse中的内容。
/**
* 根据城市url查询天气信息
*/
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;
参考技术B HttpClient ,你可以看一下这个框架,apache 的
java后台发起get请求获取响应数据学习记录: 话不多说直接上代码
:
package com.jl.chromeTest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
/**
* get请求测试
* @author liujilong
* @since 2019-7-18 10:26:49
*/
public class Test
@org.junit.Test
public void test() throws Exception
String result = get("http://www.baidu.com");
System.out.println("result====="+result);
/**
* get请求
* @param url
* @return
* @throws Exception
*/
public String get(String url) throws Exception
String content = null;
URLConnection urlConnection = new URL(url).openConnection();
HttpURLConnection connection = (HttpURLConnection) urlConnection;
connection.setRequestMethod("GET");
//连接
connection.connect(http://www.amjmh.com/v/);
//得到响应码
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader
(connection.getInputStream(), StandardCharsets.UTF_8));
StringBuilder bs = new StringBuilder();
String l;
while ((l = bufferedReader.readLine()) != null)
bs.append(l).append("\n");
content = bs.toString();
return content;
————————————————
以上是关于java获取响应数据,急问。的主要内容,如果未能解决你的问题,请参考以下文章
急问,织梦 dede 的tag标签,在删除后 前台页面仍然存在,怎么办?
java 远程访问服务端 响应头contenttype= text/html 怎么获取里面的数据