java发起http请求获取返回的Json对象方法
作者:jeanFlower 时间:2022-06-20 04:00:00
话不多说,先看代码!
/**
* Created by david on 2017-7-5.
*/
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequestUtil {
/**
* 发起http请求并获取结果
* @param requestUrl 请求地址
*/
public static JsonObject getXpath(String requestUrl){
String res="";
JsonObject object = null;
StringBuffer buffer = new StringBuffer();
try{
URL url = new URL(requestUrl);
HttpURLConnection urlCon= (HttpURLConnection)url.openConnection();
if(200==urlCon.getResponseCode()){
InputStream is = urlCon.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String str = null;
while((str = br.readLine())!=null){
buffer.append(str);
}
br.close();
isr.close();
is.close();
res = buffer.toString();
JsonParser parse =new JsonParser();
object = (JsonObject) parse.parse(res);
}
}catch(IOException e){
e.printStackTrace();
}
return object;
}
public static JsonObject postDownloadJson(String path,String post){
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(post);//post的参数 xx=xx&yy=yy
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len=bis.read(arr))!= -1){
bos.write(arr,0,len);
bos.flush();
}
bos.close();
JsonParser parse = new JsonParser();
return (JsonObject)parse.parse(bos.toString("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//测试
public static void main(String args [] ) {
JsonObject res = null;
// res = getXpath("http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42");
res = postDownloadJson("http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42","123");
System.out.println(res);
System.out.println(res.get("code"));
System.out.println(res.get("data"));
}
}
看第一个方法,发送get请求获取后台数据,其中,将返回回来的字符串解析成json对象用到了google的Gson.jar包,用到了其中JsonParser的parse方法。
第二个方法,发送post数据到后台并获取后台数据。
来源:https://blog.csdn.net/jeanFlower/article/details/74494136
标签:java,http,请求,Json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java集合定义与用法实例总结【Set、List与Map】
2023-11-21 08:26:40
![](https://img.aspxhome.com/file/2023/8/59588_0s.png)
Spring Security 强制退出指定用户的方法
2022-10-04 18:13:04
JAVA 实现磁盘文件加解密操作的示例代码
2023-11-15 00:13:06
SpringBoot整合WebService的实现示例
2023-05-25 12:37:55
![](https://img.aspxhome.com/file/2023/4/60754_0s.png)
Java集合的Collection接口和List接口详解
2021-11-02 05:24:44
基于Spring整合mybatis的mapper生成过程
2022-01-11 03:49:26
![](https://img.aspxhome.com/file/2023/9/61129_0s.png)
Java实现map转换成json的方法详解
2022-09-09 12:26:03
SpringMVC @RequestMapping注解详解
2022-08-08 06:58:14
![](https://img.aspxhome.com/file/2023/3/61293_0s.png)
Java面试题之基本语法(图解)
2023-11-24 09:37:34
![](https://img.aspxhome.com/file/2023/5/59915_0s.png)
详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)
2023-11-06 21:35:12
![](https://img.aspxhome.com/file/2023/0/58690_0s.png)
JAVA发送HTTP请求的多种方式详细总结
2021-06-15 14:43:48
Flutter 底部弹窗如何实现多项选择
2023-06-24 17:08:17
![](https://img.aspxhome.com/file/2023/7/84707_0s.gif)
spring定时任务执行两次及tomcat部署缓慢问题的解决方法
2022-12-27 09:53:25
spring中的FactoryBean代码示例
2023-11-11 18:37:20
java中单例模式讲解
2022-05-22 14:24:07
![](https://img.aspxhome.com/file/2023/5/60335_0s.png)
List集合多个复杂字段判断去重的案例
2022-08-01 16:23:28
ActiveMQ安装及部署教程图解
2023-11-17 18:01:46
![](https://img.aspxhome.com/file/2023/3/59483_0s.png)
你什么是Elastic Stack(ELK)
2022-12-02 20:12:43
![](https://img.aspxhome.com/file/2023/2/65182_0s.jpg)
java二维数组基础知识详解
2023-03-28 11:10:54
![](https://img.aspxhome.com/file/2023/9/63999_0s.png)
Springboot如何使用mybatis实现拦截SQL分页
2021-08-15 13:16:56