溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java發(fā)起http請求獲取返回的Json對象方法

發(fā)布時間:2020-09-23 13:00:07 來源:腳本之家 閱讀:161 作者:jeanFlower 欄目:編程語言

話不多說,先看代碼!

/**
 * 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對象對應的輸出流
   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)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI