初试android,写了个2个服务端springmvc基于htpp协议的json互传和传统soap协议的webservice
2016-09-08 17:32
423 查看
android没啥好看的,很简单,主要看看服务端实现吧
先看看springmvc提供的json互传核心代码
问题,本来想android客户端直接传送来json数据的,但是客户端这边接收不到,没办法只能采用流的形式传送,有大神了解这个的请赐教。
下面看看android客户端互传数据的核心代码
做的比较粗糙,欢迎指教。
附下载链接,一共3个项目,此包是打包demo,包括3个项目,1个是安卓客户端,实现登录和手机归属地查询。实现了3个接口服务,1个接口服务端提供登录是用的传统webservice,用的cxf。1个接口只是测试,测试了双向json数据传输解析。
下载点这里
手动链接
http://download.csdn.net/detail/ilovexiaou/9625437
先看看springmvc提供的json互传核心代码
@RestController public class Text { @RequestMapping(value = "/find/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<User> findapp(@PathVariable("id") long id){ User aa = new User(); aa.setId(id); aa.setAge(18); aa.setName("admin"); aa.setSalary(11.11); System.out.println(aa.getId()); return new ResponseEntity<User>(aa, HttpStatus.OK); } @RequestMapping(value = "/find/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<User> getapp(@PathVariable("id") long id,HttpServletRequest request){ //这个是post请求,先接受客户端发送过来的json数据 try { BufferedInputStream in = new BufferedInputStream(request.getInputStream()); String readString = NetUtils.readString(in); System.out.println(readString); JSONObject json = JSONObject.fromObject(readString); String name = (String) json.get("name"); System.out.println(name); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //回传客户端的json数据 User aa = new User(); aa.setId(id); aa.setAge(222); aa.setName("芝麻开门"); aa.setSalary(22.22); System.out.println(aa.getId()+100); return new ResponseEntity<User>(aa, HttpStatus.OK); } }
问题,本来想android客户端直接传送来json数据的,但是客户端这边接收不到,没办法只能采用流的形式传送,有大神了解这个的请赐教。
下面看看android客户端互传数据的核心代码
/** * 与客户端互传jsonֵ */ public static final String REST_SERVICE_URI = "http://10.0.2.2:8111/WebServiceHelloAndroid/find/115"; public void text() throws MalformedURLException, IOException, JSONException{ HttpURLConnection connection = (HttpURLConnection) new URL(REST_SERVICE_URI).openConnection(); //连接设置,我这里是post提交,记得把dooutput射程true,要不会报错。 connection.setConnectTimeout(5000); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("User-Agent", "Fiddler"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Charset", "UTF-8"); //向服务器写出json数据 OutputStream out=connection.getOutputStream(); User user=new User(); user.setId(666); user.setName("吓死你"); JSONObject jb=new JSONObject(); jb.put("id", user.getId()); jb.put("name", user.getName()); String content = String.valueOf(jb); out.write(content.getBytes()); out.flush(); out.close(); //接收服务器传过来的json数据 InputStream in = connection.getInputStream(); String json = NetUtils.readString(in); in.close(); //处理传过来的json,这个自己写逻辑去吧,我这就测试下不作操作。 JSONObject jsonObject = new JSONObject(json); User p= new User(); String username =jsonObject.getString("name"); p.setName(username); System.out.println(p.getName()); }
做的比较粗糙,欢迎指教。
附下载链接,一共3个项目,此包是打包demo,包括3个项目,1个是安卓客户端,实现登录和手机归属地查询。实现了3个接口服务,1个接口服务端提供登录是用的传统webservice,用的cxf。1个接口只是测试,测试了双向json数据传输解析。
下载点这里
手动链接
http://download.csdn.net/detail/ilovexiaou/9625437
相关文章推荐
- Android之webservice,基于soap协议的数据处理
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- android如何利用基于Http 协议的WebService服务来获取远程数据库数据
- Android WebService(基于SOAP协议) 连接方法
- Android平台调用WebService详解(基于SOAP协议)
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- android客户端与服务器交互数据(基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合)
- Android WebService(基于SOAP协议)个人记录总结
- 使用maven构建基于spring、springMVC的简单Android登陆——服务端
- SpringMvc 服务端发布json数据+android客户端解析json数据
- Android之WebService,基于Soap协议的数据处理
- Android WebService(基于SOAP协议)
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- 【android】基于Android的搜索客户端应用团队项目(服务端)
- 基于Axis2开发WebService-服务端
- Android上基于JSON的数据交互应用
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- 创建一个基于ASP.NET 2.0的JSON WebService