Commit d244a372160eb3a7858bfbe50e8c53c42447c8c0
1 parent
92983a20
u
Showing
1 changed file
with
40 additions
and
9 deletions
00.公共组件/1.3sdk.md
| @@ -12,24 +12,55 @@ public class Example { | @@ -12,24 +12,55 @@ public class Example { | ||
| 12 | //appId,appKey 到http://open.cnlive.com 后台获取。 | 12 | //appId,appKey 到http://open.cnlive.com 后台获取。 |
| 13 | String appId = "appId"; | 13 | String appId = "appId"; |
| 14 | String appKey = "appKey"; | 14 | String appKey = "appKey"; |
| 15 | - Map<String,String> params = new HashMap<>(); | ||
| 16 | - params.put("appId", appId); | 15 | + Map<String,String> map = new HashMap<>(); |
| 16 | + map.put("appId", appId); | ||
| 17 | 17 | ||
| 18 | //添加业务参数 | 18 | //添加业务参数 |
| 19 | - params.put("param1", ""); | ||
| 20 | - params.put("param2", ""); | ||
| 21 | - params.put("param3", ""); | 19 | + map.put("param1", ""); |
| 20 | + map.put("param2", ""); | ||
| 21 | + map.put("param3", ""); | ||
| 22 | 22 | ||
| 23 | String api = "http://api.cnlive.com/open/api2/..."; | 23 | String api = "http://api.cnlive.com/open/api2/..."; |
| 24 | + | ||
| 25 | + | ||
| 24 | //生成 get 请求方法的url | 26 | //生成 get 请求方法的url |
| 25 | - String url_get = OpenUtil.buildURL(api, params, appKey); | 27 | + String url_get = OpenUtil.buildURL(api, map, appKey); |
| 26 | System.out.println(url_get); | 28 | System.out.println(url_get); |
| 27 | 29 | ||
| 28 | - //生成 post 请求方法中的sign参数 | ||
| 29 | - String sign = OpenUtil.sign(params, appKey); | 30 | + //post 方式调用 |
| 31 | + String sign = OpenUtil.sign(map, appKey); | ||
| 30 | System.out.println(sign); | 32 | System.out.println(sign); |
| 33 | + map.put("sign", sign); | ||
| 31 | 34 | ||
| 32 | - //可以使用apache HttpClient 工具调用。 | 35 | + //使用apache HttpClient 工具调用。 httpclient 4.3.6 |
| 36 | + CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
| 37 | + try { | ||
| 38 | + RequestBuilder builder = RequestBuilder.post().setUri(url); | ||
| 39 | + List<NameValuePair> params = new ArrayList<NameValuePair>(); | ||
| 40 | + for (String k : map.keySet()) { | ||
| 41 | + params.add(new BasicNameValuePair(k,map.get(k))); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + builder.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); | ||
| 45 | + CloseableHttpResponse rep = httpClient.execute(builder.build()); | ||
| 46 | + try { | ||
| 47 | + //输出,或转化为java bean对象 | ||
| 48 | + System.out.println(EntityUtils.toString(rep.getEntity())); | ||
| 49 | + } catch (Exception e) { | ||
| 50 | + e.printStackTrace(); | ||
| 51 | + } finally { | ||
| 52 | + rep.close(); | ||
| 53 | + } | ||
| 54 | + } catch (Exception e) { | ||
| 55 | + // TODO Auto-generated catch block | ||
| 56 | + e.printStackTrace(); | ||
| 57 | + } finally { | ||
| 58 | + try { | ||
| 59 | + httpClient.close(); | ||
| 60 | + } catch (IOException e) { | ||
| 61 | + e.printStackTrace(); | ||
| 62 | + } | ||
| 63 | + } | ||
| 33 | } | 64 | } |
| 34 | } | 65 | } |
| 35 | 66 |