初试android,写了个2个服务端springmvc基于htpp协议的json互传和传统soap协议的webservice

来源:互联网 发布:淘宝怎么举报假冒商品 编辑:程序博客网 时间:2024/05/16 03:36

android没啥好看的,很简单,主要看看服务端实现吧
先看看springmvc提供的json互传核心代码

@RestControllerpublic 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

0 0
原创粉丝点击