java發(fā)起http請(qǐng)求獲取返回的Json對(duì)象方法
話不多說,先看代碼!
/**
* 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 {
/**
* 發(fā)起http請(qǐng)求并獲取結(jié)果
* @param requestUrl 請(qǐng)求地址
*/
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);//連接超時(shí) 單位毫秒
// conn.setReadTimeout(2000);//讀取超時(shí) 單位毫秒
// 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 發(fā)送請(qǐng)求參數(shù)
printWriter.write(post);//post的參數(shù) xx=xx&yy=yy
// flush輸出流的緩沖
printWriter.flush();
//開始獲取數(shù)據(jù)
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;
}
//測(cè)試
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"));
}
}
看第一個(gè)方法,發(fā)送get請(qǐng)求獲取后臺(tái)數(shù)據(jù),其中,將返回回來的字符串解析成json對(duì)象用到了google的Gson.jar包,用到了其中JsonParser的parse方法。
第二個(gè)方法,發(fā)送post數(shù)據(jù)到后臺(tái)并獲取后臺(tái)數(shù)據(jù)。
以上這篇java發(fā)起http請(qǐng)求獲取返回的Json對(duì)象方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring boot如何快速的配置多個(gè)Redis數(shù)據(jù)源
這篇文章主要介紹了Spring boot如何快速的配置多個(gè)Redis數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
SpringBoot內(nèi)置數(shù)據(jù)源的持久化與解決方案
數(shù)據(jù)源的配置 我們先基于SpringBoot默認(rèn)的HikariDataSource數(shù)據(jù)源,導(dǎo)入JDBC場(chǎng)景,看看SpringBoot幫我們自動(dòng)配置了什么,下面我們來了解SpringBoot內(nèi)置數(shù)據(jù)源持久化2022-07-07
學(xué)習(xí)Java之IO流中有哪些復(fù)雜的API
這篇文章我們要先對(duì)IO流的API有個(gè)基本的認(rèn)知,因?yàn)镮O流的類和方法太多了,我們不得不專門學(xué)習(xí)一下,所以本文就給大家詳細(xì)的講講Java?IO流中復(fù)雜的API,需要的朋友可以參考下2023-09-09
Spring Boot整合mybatis使用注解實(shí)現(xiàn)動(dòng)態(tài)Sql、參數(shù)傳遞等常用操作(實(shí)現(xiàn)方法)
這篇文章主要介紹了Spring Boot整合mybatis使用注解實(shí)現(xiàn)動(dòng)態(tài)Sql、參數(shù)傳遞等常用操作(實(shí)現(xiàn)方法),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

