Android 调用 Smart Framework构建的Rest服务
来源:互联网 发布:网络报装 编辑:程序博客网 时间:2024/06/06 00:13
首先感谢下 黄勇 大神 写出这么好用的javaweb框架。
Smart Framework详细内容请点击:http://www.oschina.net/p/smart-framework
首先使用smart建立Rest服务的方法,个人分析可用两种方式:
第一种,直接在service层上创建Rest服务
服务端:
@Service@Rest("/userservice")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)public class UserServiceImpl implements UserService { @POST @Path("/login/{username}/{password}") @Override public void login(@PathParam("username") String username, @PathParam("password") String password) { SecurityHelper.login(username, password, false); }}
android客户端:
public static String sendRequest(HttpRequestMethod methodEnum, String url, Map<String, String> paramsMap) { String result = null; HttpUriRequest req = methodEnum.createRequest(url); req.addHeader("Content-Type",MediaType.APPLICATION_JSON); try { if (paramsMap != null) { JSONObject jsonObj = new JSONObject(paramsMap); StringEntity entity = new StringEntity(jsonObj.toString()); ((HttpEntityEnclosingRequest)req).setEntity(entity); } HttpResponse httpResp = httpClient.execute(req); if (httpResp.getStatusLine().getStatusCode() == HTTP_200) { result = EntityUtils.toString(httpResp.getEntity(), DEFAULT_LOCAL_ENCODE); } else { } } catch (Exception e) { e.printStackTrace(); } return result; } public static String login(String username ,String password){ return sendRequest(HttpRequestMethod.HttpPost,RootUrl+"/rest/userservice/login/"+username+"/"+password,null); }
第二中是在action层上建立Rest服务,这种其实不用建立Rest服务,直接调用action地址即可
直接上android端代码
public static String sendRequest(HttpRequestMethod methodEnum, String url, Map<String, String> paramsMap) { String result = null; HttpUriRequest req = methodEnum.createRequest(url); try { if (paramsMap != null) { <span style="color:#ff0000;"><strong>((HttpEntityEnclosingRequest)req).setEntity(new UrlEncodedFormEntity( keyValueToValuePairList(paramsMap), DEFAULT_REMOTE_ENCODE));</strong></span> } HttpResponse httpResp = httpClient.execute(req); if (httpResp.getStatusLine().getStatusCode() == HTTP_200) { result = EntityUtils.toString(httpResp.getEntity(), DEFAULT_LOCAL_ENCODE); } else { } } catch (Exception e) { e.printStackTrace(); } return result; } public static String login(String username ,String password){ Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put("username", "admin"); paramsMap.put("password", "admin"); return sendRequest(HttpRequestMethod.HttpPost,RootUrl+"/login,paramsMap); }
0 0
- Android 调用 Smart Framework构建的Rest服务
- 构建Scala的REST服务
- 使用restify构建纯粹的rest服务
- Jersey构建REST服务实战
- Jersey构建REST服务实战
- Jersey构建REST服务实战
- Jersey构建REST服务实战
- 在Node.js中构建正确的REST Web服务
- 根据官网构建Scala的Rest服务
- android framework重要的服务
- 在Android开发中调用Rest web服务
- 在Android开发中调用Rest web服务
- 在Android开发中调用Rest web服务
- 在Android开发中调用Rest web服务
- 部署在wcf rest服务上的wcf rest服务调用页面程序
- smart-framework
- ArcGIS Engine 调用Rest服务
- JQuery调用Rest服务接口
- 【02】 机器学习(Standford)笔记 L02
- 【课后作业】第五章概念总结 .
- Java的IO流架构
- 比较有技巧的设计思想
- spring 简单IOC测试
- Android 调用 Smart Framework构建的Rest服务
- 用velocity生成代码,中文乱码问题
- cocos2d-lua
- Linux下那些奇葩的命令
- 通达OA 公告管理模块增加管理项目,使用OA系统组件开发(图文)
- 富受贿数额为45437元
- NS2的分组包结构分析
- 深入C++ string.find()函数的用法总结
- 何解决SQL Server 2008 无法连接到(local)