SMP学习笔记之使用REST API在任何操作系统上消费SMP的OData服务

来源:互联网 发布:水泥自流平环保吗 知乎 编辑:程序博客网 时间:2024/05/31 20:51

因为项目需要最近学习了使用SMP REST API的方式使用SMP的服务,首先是一个标准版的webservice,http://192.168.99.35:8088/openPlatform/webservice/rest?_wadl

然后进入SMP管理后台,创建一个application,如图:


保存设置,然后ping看看能否成功:


好的,SMP端的配置结束了。接下来就要在程序中消费这些服务了。

移动端要使用SMP的服务一共有两个步骤,一是注册,而且调用接口获取数据

先看注册,先使用postman来调试(至于postman的安装,百度去吧):

地址的格式是http(s)://[host]:[port]/odata/applications/latest/[application id]/Connections,默认的端口是8080

postman如图:


成功的话返回如下截图:


使用浏览器注册的话会有缓存,如果进行再次注册将无法成功,需要清除浏览器的缓存才能再次注册,当有了缓存之后进行后面的数据获取操作时就不需要传某些认证的数据了

接下来使用postman发送一个post请求获取app的版本信息

地址的格式为:http(s)://[host]:[port]/[application id]/[目标webservice后面的地址]

方式为post,headers里面的Authorization信息是使用basic Auth方式生产的base64的编码,帐号密码随便填,因为咱们前面设置SMP的认证方式的时候选择的是没有认证

然后使用正常调用webservice的方式传入相应数据发送请求得到返回的结果

到目前为止咱们的SMP后端都配置完成并且已经知道如何去注册和获取数据了,接下来要使用代码来实现,在下因为做android的项目因此使用的是java语言。


注册的核心代码:

/** * Post注册SMP的方式提交 *  *  * @throws Exception */public static String postRegister() throws Exception {// 创建HttpClient实例HttpClient httpclient = getDefaultHttpClient("UTF-8");StringEntity entitys = new StringEntity(SMPModelConstants.REGISTER_ENTITY);entitys.setContentType("application/xml");HttpPost hp = new HttpPost(DataProviderFactory.REGISTER_URL);hp.setEntity(entitys);hp.setHeader("Content-type", "application/xml");String responseStr = null;try {HttpResponse response = httpclient.execute(hp);StringBuilder sb=new StringBuilder();for (Header header : response.getHeaders("Set-Cookie")) {if (header != null) {String cookie = header.getValue();cookie=cookie.substring(0, cookie.indexOf(";")+1);sb.append(cookie);}}DataProviderFactory.setCookies(sb.toString());// responseStr = httpclient.execute(hp, responseHandler);} catch (ClientProtocolException e) {throw new Exception("客户端连接协议错", e);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {abortConnection(hp, httpclient);}return responseStr;}

DataProviderFactory.REGISTER_URL=前面postman注册的地址
SMPModelConstants.REGISTER_ENTITY的信息如下:
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><entryxmlns="http://www.w3.org/2005/Atom"xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"><category term="applications.Connection"scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/><content type="application/xml"><m:properties></m:properties></content></entry>


一定要记得把缓存保存起来,因为后面请求数据的时候会用到

下面是请求数据的核心代码:

/** * Post方式提交,URL中不包含提交参数, 格式:http://www.g.cn. *  * @param url *            提交地址 * @param params *            提交参数集, 键/值对 * @return 响应消息 * @throws Exception */public static String smpPost(String url, Map<String, String> params) throws Exception {if (url == null) {return null;}// 创建HttpClient实例DefaultHttpClient httpclient =getDefaultHttpClient("UTF-8");UrlEncodedFormEntity formEntity = null;try {List<NameValuePair> qparams = getParamsList(params);if (qparams != null && qparams.size() > 0) {formEntity = new UrlEncodedFormEntity(qparams, "UTF-8");}} catch (UnsupportedEncodingException e) {throw new Exception("不支持的编码集", e);}HttpPost hp = new HttpPost(url);hp.addHeader(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2438.3 Safari/537.36"));hp.addHeader(new BasicHeader("Accept","*/*"));hp.addHeader(new BasicHeader("Accept-Language","zh-cn,zh;q=0.8"));hp.addHeader(new BasicHeader("Accept-Encoding","gzip,deflate"));hp.setHeader("Content-Type", "application/x-www-form-urlencoded");hp.addHeader(new BasicHeader("Cookie", DataProviderFactory.getCookies()));hp.setHeader("Authorization", "Basic cXFxOnFxcQ==");hp.setEntity(formEntity);// 发送请求,得到响应String responseStr = null;try {responseStr = httpclient.execute(hp, responseHandler);} catch (ClientProtocolException e) {throw new Exception("客户端连接协议错误", e);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {abortConnection(hp, httpclient);}return responseStr;}

因为在下不是很懂base64加密编码那块,而且也没有设置验证,因此Authorization就随便写死了

这下就大功告成了



0 0