JAVA每月星座运势api调用代码实例

代码描述:基于JAVA每月运势api调用代码实例

接口地址:http://www.juhe.cn/docs/api/id/58

  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.net.URLEncoder;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12.  
  13. import net.sf.json.JSONObject;
  14.  
  15. /**
  16. *星座运势调用示例代码 - 聚合数据
  17. *在线接口文档:http://www.juhe.cn/docs/58
  18. **/
  19.  
  20. public class JuheDemo {
  21.     public static final String DEF_CHATSET = "UTF-8";
  22.     public static final int DEF_CONN_TIMEOUT = 30000;
  23.     public static final int DEF_READ_TIMEOUT = 30000;
  24.     public static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
  25.  
  26.     //配置您申请的KEY
  27.     public static final String APPKEY ="*************************";
  28.  
  29.     //1.运势查询
  30.     public static void getRequest1(){
  31.         String result =null;
  32.         String url ="http://web.juhe.cn:8080/constellation/getAll";//请求接口地址
  33.         Map params = new HashMap();//请求参数
  34.             params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
  35.             params.put("consName","");//星座名称,如:白羊座
  36.             params.put("type","");//运势类型:today,tomorrow,week,nextweek,month,year
  37.  
  38.         try {
  39.             result =net(url, params, "GET");
  40.             JSONObject object = JSONObject.fromObject(result);
  41.             if(object.getInt("error_code")==0){
  42.                 System.out.println(object.get("result"));
  43.             }else{
  44.                 System.out.println(object.get("error_code")+":"+object.get("reason"));
  45.             }
  46.         } catch (Exception e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.  
  53.     }
  54.  
  55.     /**
  56.      *
  57.      * @param strUrl 请求地址
  58.      * @param params 请求参数
  59.      * @param method 请求方法
  60.      * @return  网络请求字符串
  61.      * @throws Exception
  62.      */
  63.     public static String net(String strUrl, Map params,String method) throws Exception {
  64.         HttpURLConnection conn = null;
  65.         BufferedReader reader = null;
  66.         String rs = null;
  67.         try {
  68.             StringBuffer sb = new StringBuffer();
  69.             if(method==null || method.equals("GET")){
  70.                 strUrl = strUrl+"?"+urlencode(params);
  71.             }
  72.             URL url = new URL(strUrl);
  73.             conn = (HttpURLConnection) url.openConnection();
  74.             if(method==null || method.equals("GET")){
  75.                 conn.setRequestMethod("GET");
  76.             }else{
  77.                 conn.setRequestMethod("POST");
  78.                 conn.setDoOutput(true);
  79.             }
  80.             conn.setRequestProperty("User-agent", userAgent);
  81.             conn.setUseCaches(false);
  82.             conn.setConnectTimeout(DEF_CONN_TIMEOUT);
  83.             conn.setReadTimeout(DEF_READ_TIMEOUT);
  84.             conn.setInstanceFollowRedirects(false);
  85.             conn.connect();
  86.             if (params!= null && method.equals("POST")) {
  87.                 try {
  88.                     DataOutputStream out = new DataOutputStream(conn.getOutputStream());
  89.                         out.writeBytes(urlencode(params));
  90.                 } catch (Exception e) {
  91.                     // TODO: handle exception
  92.                 }
  93.             }
  94.             InputStream is = conn.getInputStream();
  95.             reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
  96.             String strRead = null;
  97.             while ((strRead = reader.readLine()) != null) {
  98.                 sb.append(strRead);
  99.             }
  100.             rs = sb.toString();
  101.         } catch (IOException e) {
  102.             e.printStackTrace();
  103.         } finally {
  104.             if (reader != null) {
  105.                 reader.close();
  106.             }
  107.             if (conn != null) {
  108.                 conn.disconnect();
  109.             }
  110.         }
  111.         return rs;
  112.     }
  113.  
  114.     //将map型转为请求参数型
  115.     public static String urlencode(Map<String,Object>data) {
  116.         StringBuilder sb = new StringBuilder();
  117.         for (Map.Entry i : data.entrySet()) {
  118.             try {
  119.                 sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
  120.             } catch (UnsupportedEncodingException e) {
  121.                 e.printStackTrace();
  122.             }
  123.         }
  124.         return sb.toString();
  125.     }
  126. }

来自:http://outofmemory.cn/code-snippet/78577/JAVA-meiyue-yunshi-api-call-code-example


发布日期:

所属分类: Java 标签: