5.示例

<br/><p>以下给出java版本的示例(修改APPKEY后可以直接运行体验): ~~~ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Random; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ChatApi { private static final String AUTH_URL = "https://dev.lingju.ai/httpapi/authorize.do?appkey=%s&userid=%s&authcode=%s"; private static final String CHAT_URL = "https://dev.lingju.ai/httpapi/ljchat.do"; private static String token = "";//获取一次,有效期为30天// private static final String APIKEY = "62dd9f0c0e606115aede6e28d8684904";//"a776d26cfc67698b319487b78583dbf7"; private static String AUTH_CODE="1b21bb8f726bd93b90da46633a8ad4e1"; public static void getAccessToken(String userid){ try { String jsonstring=get(String.format(AUTH_URL, APIKEY,userid,AUTH_CODE)); System.out.println("accessToken_json>>"+jsonstring); //请开发者使用自己熟悉的json框架解析,如java的:gson,jackson,json-org等。其它平台类似。此处为了不引入第三方jar包,直接用正则提取accessToken Pattern pt=Pattern.compile("\"accessToken\"\\:\"(\\w+)\""); Matcher mt=pt.matcher(jsonstring); if(mt.find()){ token=mt.group(1); System.out.println("token="+token); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static String get(String url) throws IOException{ System.out.println("get>>"+url); URL u=new URL(url); HttpURLConnection conn=(HttpURLConnection) u.openConnection(); conn.setConnectTimeout(10*1000); conn.setRequestMethod("GET"); try{ InputStream in = conn.getInputStream(); if(conn.getResponseCode()==200){ int l=conn.getContentLength(); byte bytes[]=new byte[l]; in.read(bytes, 0, l); in.close(); return new String(bytes,"UTF-8"); } else in.close(); } catch(Exception e){ e.printStackTrace(); }finally{ conn.disconnect(); } return null; } static String chatJson="{"+ "\"accessToken\":\"%s\","+//必填 "\"input\":\"%s\","+//必填 "\"lat\":\"23.12635\","+//非必填 "\"lng\":\"113.38776\","+//非必填 "\"city\":\"广州\","+//非必填 "\"position\":\"广东广州(非必填)\""+ "}"; public static String getAnswer(String text) throws IOException{ URL u=new URL(CHAT_URL); HttpURLConnection conn=(HttpURLConnection) u.openConnection(); conn.setConnectTimeout(10*1000); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); OutputStream out= conn.getOutputStream(); try{ String json=String.format(chatJson, token,text); out.write(json.getBytes("UTF-8")); out.close(); out=null; InputStream in = conn.getInputStream(); if(conn.getResponseCode()==200){ int l=conn.getContentLength(); byte bytes[]=new byte[l]; in.read(bytes, 0, l); in.close(); conn.disconnect(); return new String(bytes,"UTF-8"); } else in.close(); }catch(Exception e){ e.printStackTrace(); } finally{ if(out!=null){ out.close(); } conn.disconnect(); } return null; } //获取酷我音乐歌曲播放链接的模版 static String KUWO_FETCHED_URL_TEMPLATE= "http://antiserver.kuwo.cn/anti.s?response=url&format=aac%smp3&type=convert_url&rid=%s&qq-pf-to=pcqq.c2c"; public static String getMusicUrlBy(String musicId) throws IOException{ String result=null; URL urls = new URL(String.format(KUWO_FETCHED_URL_TEMPLATE,"%7C",musicId)); HttpURLConnection conn = (HttpURLConnection) urls.openConnection(); try { conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "*/*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setConnectTimeout(10000); conn.connect(); if(conn.getResponseCode()==200){ InputStream in = conn.getInputStream(); // 通过输入流获取html二进制数据 int l=conn.getContentLength(); byte bytes[]=new byte[l]; in.read(bytes, 0, l); in.close(); result=new String(bytes,"UTF-8"); } conn.disconnect(); if(result!=null&&result.trim().startsWith("http")){ if(result.indexOf("?")>-1){ result=result.substring(0,result.lastIndexOf("?")); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); result=null; } return result; } public static void main(String[] args) throws Exception { getAccessToken("11111111"); Scanner sc=new Scanner(System.in); String input; System.out.print("input>>"); while(sc.hasNext()){ input=sc.nextLine(); if("exit".equals(input)){ break; } if("test".equals(input)){ for(int i=0;i<20;i++){ new Thread(new Runnable() { @Override public void run() { try { System.out.println(getAnswer(new Random().nextInt(100)+"testtesttest")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } else{ System.out.println(getAnswer(input)); } System.out.print("input>>"); } sc.close(); //System.out.println(getMusicUrlBy("MUSIC_157093")); } } ~~~