이 기사에서는 Java Monthly Fortune API Call Code를 참조에 대해 공유합니다. 특정 내용은 다음과 같습니다
import java.io.bufferedReader; import java.io.dataOutputStream; import java.io.ioException; import java.io.inputStreamReader; import java.io.unsupportedEncodingException; import java.net.httpurlConnection; import java.net.url; java.util.hashmap; import java.util.map; import java.util.map; import java.util.map; import net.sf.json.jsonobject; /*** 별자리 호출에 대한 샘플 코드 - 집계 데이터*온라인 인터페이스 문서 : http://www.juhe.cn/docs/58**/ public class juhedemo {public static final String def_chatset = "UTF -8"; 공개 정적 최종 최종 INT DEF_CONN_TIMEOUT = 30000; 공개 정적 최종 int def_read_timeout = 30000; public static string userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (Gecko Like Gecko) Chrome/29.0.1547.66 Safari/537.36"; // 주요 공개 정적 최종 문자열 appkey = "***************************"; // 1. Fortune Query public static void getRequest1 () {문자열 result = null; 문자열 url = "http://web.juhe.cn:8080/constellation/getall"; // 인터페이스 주소 맵 params = new hashmap (); // request parameters paramets.put ( "key", appkey); // appkey (자세한 페이지 쿼리 적용) params.put ( "consname", ""); // constellation name, 예 : Aries params.put ( "type", ""); intertune 유형, Month, Year, Year, Month, Year recond, Month, Apport recten, Month, Apportation Name, Apply Appkey (자세한 페이지 쿼리 적용) params.put ( "consname", ""); // constellation name 매개 변수, "get"); jsonobject object = jsonobject.fromobject (결과); if (object.getInt ( "error_code") == 0) {system.out.println (Object.get ( "result")); } else {system.out.println (object.get ( "error_code")+":"+object.get ( "rasy"); }} catch (예외 e) {e.printstacktrace (); }} public static void main (string [] args) {} / ******** @param strurl 요청 주소* @param params 요청 매개 변수* @param 메소드 요청 방법* @return 네트워크 요청 문자열* @throws 예외* / public static string net (String strurl, map params, string method) {httpurlconnection conn = null; bufferedReader reader = null; 문자열 rs = null; try {StringBuffer sb = new StringBuffer (); if (method == null || method.equals ( "get")) {strurl = strurl+"?"+urlencode (params); } url url = new URL (strurl); conn = (httpurlConnection) url.openConnection (); if (method == null || method.equals ( "get")) {conn.setRequestMethod ( "get"); } else {conn.setRequestMethod ( "post"); conn.setdooutput (true); } conn.setRequestProperty ( "사용자 에이전트", UserAgent); Conn.setUeCaches (False); Conn.setConnectTimeout (def_conn_timeout); conn.setReadTimeout (def_read_timeout); Conn.SetInstanceFollerDirects (False); 연결하다(); if (params! = null && method.equals ( "post")) {try {dataOutputStream out = new DataOutputStream (conn.getOutputStream ()); out.writeBytes (urlencode (params)); } catch (예외 e) {// todo : handle exception}} inputStream은 = conn.getInputStream (); reader = new bufferedReader (new inputStreamReader (is, def_chatset)); 문자열 strread = null; while ((strread = reader.readline ())! = null) {sb.append (strread); } rs = sb.toString (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (reader! = null) {reader.close (); } if (conn! = null) {conn.disconnect (); }} return rs; } // 맵 유형을 요청합니다. 매개 변수 유형 public static string urlencode (map <string, object> data) {StringBuilder sb = new StringBuilder (); for (map.entry i : data.entryset ()) {try {sb.append (i.getkey ()). Append ( "="). Append (urlencoder.encode (i.getValue ()+"", "UTF-8"). Append ( "&"); } catch (UnsupportedEncodingException e) {e.printstacktrace (); }} return sb.toString (); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.