基于JAVA每月运势api调用代码实例
程序员文章站
2024-03-13 16:08:27
本文实例为大家分享了java每月运势api调用代码,供大家参考,具体内容如下
import java.io.bufferedreader;
import j...
本文实例为大家分享了java每月运势api调用代码,供大家参考,具体内容如下
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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 完整B树算法Java实现代码
下一篇: Java 开发的几个注意点总结