您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 商业计划书 > 基于JAVA的网上阅读api调用代码实例
基于JAVA的网上阅读api调用代码实例代码描述:基于JAVA的网上阅读api调用代码实例代码平台:聚合数据importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.util.HashMap;importjava.util.Map;importnet.sf.json.JSONObject;/***数字阅读信息调用示例代码-聚合数据*在线接口文档:**/publicclassJuheDemo{publicstaticfinalStringDEF_CHATSET=UTF-8;publicstaticfinalintDEF_CONN_TIMEOUT=30000;publicstaticfinalintDEF_READ_TIMEOUT=30000;publicstaticStringuserAgent=Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/29.0.1547.66Safari/537.36;//配置您申请的KEYpublicstaticfinalStringAPPKEY=*************************;//1.获取排行类型接口publicstaticvoidgetRequest1(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//应用APPKEYtry{result=net(url,params,GET);JSONObjectobject=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(Exceptione){e.printStackTrace();}}//2.获取排行内容数据接口publicstaticvoidgetRequest2(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//应用APPKEYparams.put(contentType,);//内容类型ID,可通过调用“获取排行类型信息接口”获取params.put(rankType,);//排行类型ID,可通过调用“获取排行类型信息接口”获取params.put(rankTime,);//排行时间类型ID,可通过调用“获取排行类型信息接口”获取params.put(start,);//获取数据的起始条数,默认为0params.put(count,);//获取数据的总条数,默认为18params.put(copyRight,);//内容书籍的版权,不传参数默认获取所有书籍,不对版权进行过滤1、有线版权2、无线版权3、无转授权4、无信息网络传播权try{result=net(url,params,GET);JSONObjectobject=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(Exceptione){e.printStackTrace();}}//3.获取推荐类型publicstaticvoidgetRequest3(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//应用APPKEY(应用详细页查询)params.put(dtype,);//返回数据的格式,xml或json,默认jsontry{result=net(url,params,GET);JSONObjectobject=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(Exceptione){e.printStackTrace();}}//4.获取热门搜索推荐publicstaticvoidgetRequest4(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请的appkeytry{result=net(url,params,GET);JSONObjectobject=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(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){}/****@paramstrUrl请求地址*@paramparams请求参数*@parammethod请求方法*@return网络请求字符串*@throwsException*/publicstaticStringnet(StringstrUrl,Mapparams,Stringmethod)throwsException{HttpURLConnectionconn=null;BufferedReaderreader=null;Stringrs=null;try{StringBuffersb=newStringBuffer();if(method==null||method.equals(GET)){strUrl=strUrl+?+urlencode(params);}URLurl=newURL(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{DataOutputStreamout=newDataOutputStream(conn.getOutputStream());out.writeBytes(urlencode(params));}catch(Exceptione){//TODO:handleexception}}InputStreamis=conn.getInputStream();reader=newBufferedReader(newInputStreamReader(is,DEF_CHATSET));StringstrRead=null;while((strRead=reader.readLine())!=null){sb.append(strRead);}rs=sb.toString();}catch(IOExceptione){e.printStackTrace();}finally{if(reader!=null){reader.close();}if(conn!=null){conn.disconnect();}}returnrs;}//将map型转为请求参数型publicstaticStringurlencode(MapString,Objectdata){StringBuildersb=newStringBuilder();for(Map.Entryi:data.entrySet()){try{sb.append(i.getKey()).append(=).append(URLEncoder.encode(i.getValue()+,UTF-8)).append(&);}catch(UnsupportedEncodingExceptione){e.printStackTrace();}}returnsb.toString();}}
本文标题:基于JAVA的网上阅读api调用代码实例
链接地址:https://www.777doc.com/doc-2570186 .html