[英]How to communicate to a RESTful Webservice in Android Client
我已经使用(Java + Jersey)开发了一个rest webservice ...该服务需要与用户进行通信,即用户需要在android客户端应用中填写表格并将其发送回rest webservice,然后在其中进行处理,用户将获得输出...
当我第一次使用其余的Web服务时,我使用了一个Webclient ...,因此能够以“ post”和“ get”的形式参数发送和接收请求...
但是如何在android中做同样的事情,因为没有表单标签具有诸如method="post"
类的属性
我被指示使用XML解析。
更新:我知道如何从REST Web服务中以ANDROID连接和获取数据,但如何发布它呢?
我的Form方法,它接受来自Web客户端的响应:
@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");
}
}
我看到您请求了POST
方法。 这是一个可以帮助您的代码段。
String urlParameters = "param_a=a¶m_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应用程序在所有android系统中都能正常工作。
您需要Android客户端将HTTP请求发送到您的Web服务。 看一下HttpURLConnection类。 在这里您可以找到android中HTTP客户端的快速介绍。
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;
看看loopj吧,它建立在Apache httpclient之上,使网络操作变得轻而易举。 它使您可以处理json和xml,调用是异步的,并且具有get / post参数构建器,因此您可以轻松设置参数。 参见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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.