如何从Struts2调用restful Web服务?

时间:2013-09-20 09:51:07

标签: web-services struts2

我做了小申请。我做了登录申请。我想从动作类调用restful web服务。我使用了struts2框架。我无法改变宁静的网络服务。请给我一些建议。

1 个答案:

答案 0 :(得分:2)

这是我的问题的解决方案。

 try {

        URL url = new URL("http://localhost:8080/ProWebservices/webresources/users/" + emailid + "&" + password + "&photographer");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            JsonObject json = (JsonObject) new JsonParser().parse(output);
            String out = (String) json.get("userid").toString();
            if(!out.equals("null")){
                return "SUCCESS";
            }
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }