基于java的菜谱大全接口调用代码实例
基于JAVA的菜谱大全接口调用代码实例代码描述:基于」AVA的菜谱大全接口调用代码实例接口平台:聚合数据importjava•io•BufferedReader;importjava•io•DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.iOeInputStreamReader;importjava>io>UnsupportedEncodingException;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.util.HashMap;importjava
Map;importnet.sf.json.DSONObject;/***菜谱大全调用示例代码一聚合数裾*在线接口文档:http://www.juhe.cn/docs/46publicclassJuheDemo{publicstaticfinalStringDEF_CHATSET=nUTF-8";publicstaticfinalintDEF_CONN_TIMEOUT=30000;publiestaticfinalintDEF_READ_TIMEOUT=30000;StringuserAgent=HMozilla/5.0(WindowsNT6.1)Ap(KHTML,likeGecko)Chrome/29.0.1547.66Safari/537.3publicstaticpleWebKit/537.36//配置您申请的KEYpublicstaticfinalStringAPPKEY="*************************";//I.菜谱大全publicstaticvoidgetRequestl(){Stringresult=null;Stringurl="http://apis.juhe.cn/cook/query.php";//请求接口地址Mapparams=newHashMap();//请求参数params.put("menu",••");//需要查询的菜谱名params.put("key",APPKEY);//应用APPKEY(应用详细页查询)params.put("dtype","’’);//返回数据的格式,xml或json,默认jsonparams.put("pn","");//数据返回起始下标params.put("rn数掘返回条数,最大30params.put("albums","");//albums字段类型,1字符串,默认数绀try{\nresult=net(url,params,"GET");DSONObjectobject=OSONObject.fromObject(resuIt);if(object.getlnt("error_code")==0){System.out.print1n(object.get("result"));}else{System.out.println(object.get("error_code")+":"+objecget("reason"));}}catch(Exceptione){e.printstackTrace();}}//2.分类标签列表publiestaticvoidgetRequest2(){Stringresult=null;Stringurl="http://apis,juhe.cn/cook/category";//请求接口地址Mapparams=newHashMap();//请求参数params.put("parentid",•••■);//分类ID,默认全部param5.口的("1^7",八??1^丫);//应用众??仆丫(应用详细页查询)params.put("dtype","’’);//返回数据的格式,xml或json,默认jsontry{result=net(url,params,"GET”);JSONObjectobject=JSONObject.fromObject(result);if(object.getlnt("error_code")==0){System.out.println(object.get(HresultH));}else{System.out.println(object.get("error_coden)+n:"+objecget("reason"));}}catch(Exceptione){e.printStackTrace();}}//3.按标签检索菜谱publiestaticvoidgetRequest3(){Stringresult=null;Stringurl="http://apis.juhe.cn/cook/index";//请求接口地址Mapparams=newHashMap();//请求参数params.put("cid","••);//标签IDparams.put("key",APPKEY);//应用APPKEY(应用详细页查询)params.put("dtype","");//返冋数据的格式,xml或json,默认jsonparams.put("pn"/1");//数据返回起始卜H默认0\nparams.put("rn","••);//数tR•返回条数,最大30,默认10params.put(nformat",nn);//steps字段屏蔽,默认品示,format=l吋屏蔽try{result=net(url>params,"GET");ZJSONObjectobject=OSONObject.fromObject(result);if(object.getlnt("error_code")==0){System.out.println(object.get("result"));}else{System.out.println(object.get("error_code")+":"+object.get("reason"));}}catch(Exceptione){e.printStackTrace();}}//4.按菜谱ID查看详细publicstaticvoidgetRequest4(){Stringresult=null;Stringurl=uhttp://apis.juhe.cn/cook/queryid";//请求接口地址Mapparams=newHashMap();//诘求参数params.putrid"/1");//菜谱的IDparams.put("key",APPKEY);//应用APPKEY(应用详细页查询)params.put("dtype",••");//返回数裾的格式,xml或json,默认jsontry{result=net(url,params,"GET");DSONObjectobject=JSONObject.fromObject(result);if(object.getlnt("error_code")==0){System.out.println(object.get("result’1));}else{System.out.printIn(object.get("error_code")+":"+object.get("reason"));}}catch(Exceptione){e.printStackTrace();publicstaticvoidmain(String[]args){/***\n*@paramstrUrl请求地址*@paramparamsi青求参数*@parammethod请求方法*@return网络请求字符串*@throwsException*/publicstaticStringnet(StringstrUr1^Mapparams^Stringmethod)throwsException{Httpl)RLConnectionconn=null;BufferedReaderreader=null;Stringrs=nu11;try{StringBuffersb=newStringBuffer();if(method==null||method.equals("GETu)){strUrl=strUnl+',?,,+urlencode(params);}URLurl=newURL(strllrl);conn=(HttpURLConnection)url.openConnection();if(method==null||method,equals("GETu)){conn.setRequestMethod("GET");}else{conn.setRequestMethod("POST");conn.setDoOutput(true);}conn.setRequestProperty(nUser-agent",userAgent);conn.setllseCaches(false);conn•setConnectTimeout(DEF_CONN_TIMEOUT);conn.setReadTimeout(DEF_READ_TIMEOUT);conn.setlnstanceFollowRedirects(false);conn.connect();if(params!=nullequals(HPOST")){try{DataOutputStreamout=newDataOutputStream(conn.getOutputStream());outgetlnputStream();reader=newBufferedReader(newInputStreamReader(isJDEF_CHATSET));StringstrRead=null;while((strRead=reader•readLine())!=null){sbdata){StringBuildersb=newStringBuilder();for(Map.Entryi:data.entrySet()){sb.append(i
查看更多