There are many movies on Douban. Sometimes it is very convenient to check the movie information and search it. But how to view Douban movies through the interface form is a very practical function for many websites and app s. Here I attach a code of Douban Movie Information Acquisition implemented by java. Interesting to see:
package com.xiaocongjisuan.module.example; 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; public class Application { 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"; //Configure the appKey and openId you applied for public static final String APP_KEY ="yours"; public static final String OPEN_ID ="yours"; //Change map type to request parameter type public static String urlEncode(Map<String,Object> params) { if(params==null){return "";}; StringBuilder sb = new StringBuilder(); for (Map.Entry<String,Object> i : params.entrySet()) { try { sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } String r=sb.toString(); if(r.endsWith("&")){ r = r.substring(0,r.length()-1); } return r; } /** * * @param requestUrl Request address * @param params Request parameters * @param method Request method * @return Request result * @throws Exception */ public static String requestContent(String requestUrl, Map<String,Object> params,String method) throws Exception { HttpURLConnection conn = null; BufferedReader reader = null; String rs = null; try { //Assembly request link StringBuffer sb = new StringBuffer(); if(method!=null&&method.equalsIgnoreCase("get")){ requestUrl = requestUrl+"?"+urlEncode(params); } //Default get URL url = new URL(requestUrl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); if(method!=null&&method.equalsIgnoreCase("post")){ conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); } //Parameter configuration 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.equalsIgnoreCase("post")) { try { DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(urlEncode(params)); } catch (Exception e) { e.printStackTrace(); } } //Read data 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; } public static void main(String[] args) throws Exception{ String domain="http://api.xiaocongjisuan.com/"; String servlet="data/filmresource/get"; String method="get"; String requestUrl=domain+servlet; Map<String,Object> params=new HashMap<String,Object>(); params.put("appKey",APP_KEY); params.put("openId",OPEN_ID); //Variable part params.put("q","Na Zha came to the world"); params.put("field","name"); params.put("currentPage",1); params.put("pageSize",10); params.put("order","down"); params.put("sortField","rating"); String result=requestContent(requestUrl,params,method); System.out.println(result); } }
In fact, the implementation of other languages is also very simple, you can see this post: Film Information Query