java發(fā)起http請求獲取返回的Json對象方法
話不多說,先看代碼!
/**
* 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請求并獲取結(jié)果
* @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);//讀取超時 單位毫秒
// 發(fā)送POST請求必須設(shè)置如下兩行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 獲取URLConnection對象對應(yīng)的輸出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 發(fā)送請求參數(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;
}
//測試
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"));
}
}
看第一個方法,發(fā)送get請求獲取后臺數(shù)據(jù),其中,將返回回來的字符串解析成json對象用到了google的Gson.jar包,用到了其中JsonParser的parse方法。
第二個方法,發(fā)送post數(shù)據(jù)到后臺并獲取后臺數(shù)據(jù)。
以上這篇java發(fā)起http請求獲取返回的Json對象方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach
這篇文章主要介紹了MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Springboot中@ConfigurationProperties輕松管理應(yīng)用程序的配置信息詳解
通過@ConfigurationProperties注解,可以將外部配置文件中的屬性值注入到JavaBean中,簡化了配置屬性的讀取和管理,這使得SpringBoot應(yīng)用程序中配置文件的屬性值可以映射到POJO類中,實現(xiàn)類型安全的屬性訪問,此方法避免了手動讀取配置文件屬性的需要2024-10-10
SpringBoot讀取多環(huán)境配置文件的幾種方式
這篇文章主要給大家介紹了SpringBoot讀取多環(huán)境配置文件的幾種方式,文章通過代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-10-10
Java 正則表達(dá)式入門詳解(基礎(chǔ)進(jìn)階)
最近看到很多同學(xué)想要學(xué)習(xí)java正則表達(dá)式的一些知識,那么腳本之家小編就為大家介紹一下,其實正則表達(dá)式實用性很強(qiáng),處理大幅文字的時候都需要用得到,語法也大同小異2017-10-10
java將指定目錄下文件復(fù)制到目標(biāo)文件夾的幾種小方法
在Java中有多種方法可以實現(xiàn)文件的復(fù)制,這篇文章主要給大家介紹了關(guān)于java將指定目錄下文件復(fù)制到目標(biāo)文件夾的幾種小方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
java實現(xiàn)簡單的學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)簡單的學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
關(guān)于Spring注解@Async引發(fā)其他注解失效的解決
這篇文章主要介紹了關(guān)于Spring注解@Async引發(fā)其他注解失效的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

