⑴ 通過http,獲取介面的json數據,前面多了三個點
寫個servlet,將結果轉換成ArrayJson對象,列印出這個json就行,前端通過ajax去請求獲得json數據。java轉json需要用到相應的jar包,
⑵ 我想向其他的http介面發送JSON數據應該怎麼做
先把HTTP獲取的數據轉換成JSON.通過JSON直接判斷.
不然要做的手續太麻煩了,還是要自己解析字元串.增加了難度.
你獲取的是JSON標准字元串.
⑶ 怎麼開發一個http的介面給返回JSON格式
http,輸入文件類型為json
然後裡面的內容實用 json 格式就可以了
請採納!如有疑問,請及時溝通
⑷ 如何向其他的http介面發送JSON數據應該怎麼做那通過什麼技術可以實現
你說的其實就是webapi唄,對方既然接受JSON數據那肯定是公開介面定義的,你自己組織JSON數據通過ajax發給它就可以了,還需要什麼「技術」?前端天天乾的不就是這個事兒么……
⑸ java如何使用http方式調用第三方介面最好有代碼~謝謝
星號是IP地址和埠號
public class HttpUtil {
private final static Log log = LogFactory.getLog(HttpUtil.class);
public static String doHttpOutput(String outputStr,String method) throws Exception {
Map map = new HashMap();
String URL = "http://****/interface/http.php" ;
String result = "";
InputStream is = null;
int len = 0;
int tmp = 0;
OutputStream output = null;
BufferedOutputStream objOutput = null;
String charSet = "gbk";
System.out.println("URL of fpcy request");
System.out.println("=============================");
System.out.println(URL);
System.out.println("=============================");
HttpURLConnection con = getConnection(URL);
try {
output = con.getOutputStream();
objOutput = new BufferedOutputStream(output);
objOutput.write(outputStr.getBytes(charSet));
objOutput.flush();
output.close();
objOutput.close();
int responseCode = con.getResponseCode();
if (responseCode == 200) {
is = con.getInputStream();
int dataLen = is.available();
int retry = 5;
while (dataLen == 0 && retry > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
dataLen = is.available();
retry--;
log.info("未獲取到任何數據,嘗試重試,當前剩餘次數" + retry);
}
log.info("獲取到報文單位數據長度:" + dataLen);
byte[] bytes = new byte[dataLen];
while ((tmp = is.read()) != -1) {
bytes[len++] = (byte) tmp;
if (len == dataLen) {
dataLen = bytes.length + dataLen;
byte[] newbytes = new byte[dataLen];
for (int i = 0; i < bytes.length; i++) {
newbytes[i] = bytes[i];
}
bytes = newbytes;
}
}
result = new String(bytes, 0, len, charSet);
} else {
String responseMsg = "調用介面失敗,返回錯誤信息:" + con.getResponseMessage() + "(" + responseCode + ")";
System.out.println(responseMsg);
throw new Exception(responseMsg);
}
} catch (IOException e2) {
log.error(e2.getMessage(), e2);
throw new Exception("介面連接超時!,請檢查網路");
}
con.disconnect();
System.out.println("=============================");
System.out.println("Contents of fpcy response");
System.out.println("=============================");
System.out.println(result);
Thread.sleep(1000);
return result;
}
private static HttpURLConnection getConnection(String URL) throws Exception {
Map map = new HashMap();
int rTimeout = 15000;
int cTimeout = 15000;
String method = "";
method = "POST";
boolean useCache = false;
useCache = false;
HttpURLConnection con = null;
try {
con = (HttpURLConnection) new URL(URL).openConnection();
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new Exception("URL不合法!");
}
try {
con.setRequestMethod(method);
} catch (ProtocolException e) {
log.error(e.getMessage(), e);
throw new Exception("通信協議不合法!");
}
con.setConnectTimeout(cTimeout);
con.setReadTimeout(rTimeout);
con.setUseCaches(useCache);
con.setDoInput(true);
con.setDoOutput(true);
log.info("當前連接信息: URL:" + URL + "," + "Method:" + method
+ ",ReadTimeout:" + rTimeout + ",ConnectTimeOut:" + cTimeout
+ ",UseCaches:" + useCache);
return con;
}
public static void main(String[] args) throws Exception {
String xml="<?xml version=\"1.0\" encoding=\"GBK\" ?><document><txcode>101</txcode><netnumber>100001</netnumber>.........</document>";
response=HttpUtil.doHttpOutput(xml, "post");
JSONObject json= JSONObject.parseObject(response);
String retcode=json.getString("retcode");
調用這個類就能獲得返回的參數。。over.
}
}
}
⑹ Java中如何根據一個http介面獲取JSON數據,http介面是通過第三方提供,有機構私鑰,求代碼示例
使用HttpClient,參照代碼如下:
http://..com/question/1446921645999461940.html
⑺ 交易所api授權不安全,固定手機lp地此就安全了,是這樣嗎
這個說法肯定不對,安全不安全,與IP地址固定不固定沒有必然的聯系,而且如果是用數據,IP地址也是無法固定的,用WIFI,即使固定了路由器的IP地址,外網的IP地址也是無法固定的。
⑻ 如何調用http介面獲取json數據及GET/POST方式調用http介面
http介面返回的json數據,其實就是http請求後返回的http主體那一部分。
http協議規定,http頭部和http主體之間是以一個空行分割的。因為http每一行(每一行是指一個頭部欄位)是以\r\n結束的,一個空行的\r\n,再加上最後一行的結束符\r\n,一起是\r\n\r\n,也就是說,當檢測到\r\n\r\n四個字元時,下一個字元開始就是http
body的內容了。把http響應主體保存下來就是json數據了。