話不多說(shuō),先看代碼!
目前成都創(chuàng)新互聯(lián)已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、南樂(lè)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
/** * 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(); //開(kāi)始獲取數(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ù),其中,將返回回來(lái)的字符串解析成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è)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:java發(fā)起http請(qǐng)求獲取返回的Json對(duì)象方法
文章起源:http://aaarwkj.com/article18/pdhjgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站維護(hù)、微信小程序、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)