<code id="ymukc"><xmp id="ymukc">

JAVA每月運勢api調用代碼實例

jforeverg的頭像 jforeverg 37 2016-05-24 17:40 13

 基本信息

× 1   

瀏覽數: 45773

分享時間: 2 年 前

15

代碼描述:基于JAVA每月運勢api調用代碼實例

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

 

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
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";
    public static final int DEF_CONN_TIMEOUT = 30000;
    public static final int DEF_READ_TIMEOUT = 30000;
    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";

    //配置您申請的KEY
    public static final String APPKEY ="*************************";

    //1.運勢查詢
    public static void getRequest1(){
        String result =null;
        String url ="http://web.juhe.cn:8080/constellation/getAll";//請求接口地址
        Map params = new HashMap();//請求參數
            params.put("key",APPKEY);//應用APPKEY(應用詳細頁查詢)
            params.put("consName","");//星座名稱,如:白羊座
            params.put("type","");//運勢類型:today,tomorrow,week,nextweek,month,year

        try {
            result =net(url, params, "GET");
            JSONObject object = JSONObject.fromObject(result);
            if(object.getInt("error_code")==0){
                System.out.println(object.get("result"));
            }else{
                System.out.println(object.get("error_code")+":"+object.get("reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }

    /**
     *
     * @param strUrl 請求地址
     * @param params 請求參數
     * @param method 請求方法
     * @return  網絡請求字符串
     * @throws Exception
     */
    public static String net(String strUrl, Map params,String method) throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String 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("User-agent", userAgent);
            conn.setUseCaches(false);
            conn.setConnectTimeout(DEF_CONN_TIMEOUT);
            conn.setReadTimeout(DEF_READ_TIMEOUT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            if (params!= null && method.equals("POST")) {
                try {
                    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                        out.writeBytes(urlencode(params));
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }

    //將map型轉為請求參數型
    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();
    }
}

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

很好
1 年 前 liaoxq

12 [下一頁]

  • h3399.cn的頭像 h3399.cn 2018-10-16 18:14 代碼數:0

    很好

  • Johnli的頭像 Johnli 2017-01-10 14:13 代碼數:0

  • 威茲曼的頭像 威茲曼 2016-05-25 12:32 代碼數:0

  • 2472952206的頭像 2472952206 2018-01-11 17:35 代碼數:0

    棒棒噠

  • elya_siwei的頭像 elya_siwei 2016-06-25 18:17 代碼數:0

    最近剛好在找這方面相關的實例…贊

  • f_long的頭像 f_long 2016-07-03 11:36 代碼數:0

    贊...

  • banjue的頭像 banjue 2017-04-28 16:45 代碼數:0

    學到了

  • csmyg的頭像 csmyg 2017-05-04 11:03 代碼數:0

    不錯  測試測試

  • minxiang的頭像 minxiang 2016-07-22 09:22 代碼數:0

  • xtmtd的頭像 xtmtd 2016-07-23 11:21 代碼數:0

您的評論:

  
六合特码资料
<code id="ymukc"><xmp id="ymukc">
<code id="ymukc"><xmp id="ymukc">