如何与Android客户端中的RESTful Web服务进行通信

时间:2013-06-21 10:47:52

标签: java android rest jersey

我使用(Java + Jersey)开发了一个rest webservice ... 该服务需要与用户进行通信,即用户需要填写Android客户端应用程序中的表单并将其发送回休息网络服务,在那里它将被处理,因此用户将获得输出......

当我第一次创建其余的webservice时,我使用了webclient ...因此能够在表单参数中发送和接收请求为“post”和“get”......

但是如何在android中做同样的事情,因为没有像method="post"这样的属性的表单标签

我被指示使用XML解析。

更新:我知道如何从REST WEBSERVICE连接和获取数据,但我们如何发布它?

我的表单方法接受了来自webclient的响应:

@Path("/create")
    @POST
    @Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XML})
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void newUser(@FormParam("uname") String uname,
            @FormParam("password") String password,
            @Context HttpServletResponse servletResponse) throws IOException {
        boolean unamec= UserLogin.checkuname(uname);
        if (unamec) {
            // User already exists
            servletResponse.sendRedirect("http://mysite/Login.html");
        } else {
            UserLogin u = new UserLogin(uname, password);
            servletResponse.sendRedirec("http://mysite/users/"+uname+"/createnewshop");
        }
    }

4 个答案:

答案 0 :(得分:1)

我看到你请求了POST个方法。这是一个可以帮助你的片段。

        String urlParameters = "param_a=a&param_b=b";
        URL url = new URL("http://www.yourwebservice.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true); 
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setUseCaches(false);

        // Writes the content to the web service
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();

        // Reads the reply from the web service
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String decodedString;
        while ((decodedString = in.readLine()) != null) {
            returnMsg += decodedString;
        }

        in.close();   
        connection.disconnect();

我只建议您使用Async Tasks连接到HTTP,这样您就可以保证您的Android应用程序可以在所有机器人中使用。

答案 1 :(得分:0)

您需要Android客户端向您的Web服务发送HTTP请求。 看看HttpURLConnection课程。在这里,您可以快速了解HTTP client in android

答案 2 :(得分:0)

Unmarshaller um;
JAXBContext jc = JAXBContext.newInstance(YourClass.class);
um = jc.createUnmarshaller();

URL url = new URL("http://localhost:8080/NutzungscodesMekacodesRESTful/rest/nc/");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
YourClass obj = (YourClass) um.unmarshal(con.getInputStream());
con.disconnect();
model.setXxx(obj.getData());

另外,在这种情况下,您必须注释您的类(YourClass.java)。 像那样:

@XmlRootElement(name="TagName")
@XmlAccessorType(XmlAccessType.FIELD)
public class YourClassimplements Serializable
{
private static final long serialVersionUID = 1L;
@XmlAttribute
private int ID;
@XmlElement
private String name;

答案 3 :(得分:0)

看看loopj,它构建在Apaches httpclient之上,使网络操作变得轻而易举。它允许你使用json和xml,调用是异步的,它有一个get / post params构建器,因此你可以轻松设置你的参数。见http://loopj.com/android-async-http/

根据文档的简短示例:

private static final String BASE_URL = "http://api.twitter.com/1/";
private static AsyncHttpClient client = new AsyncHttpClient();

// i've skipped some of the implementation details, but in essence it boils down to this: 
client.post(url, params, responseHandler);
相关问题