Android RESTful POST在哪里?(Where does Android RESTful POST go?)

编程入门 行业动态 更新时间:2024-10-28 16:19:01
Android RESTful POST在哪里?(Where does Android RESTful POST go?)

我想首先说我是Android和java的新手(在较小程度上)。 我有一个客户端 - 服务器应用程序,客户端是一个Android应用程序,服务器运行Tomcat。 考虑到我是新手,我对POST请求有点困惑。 客户端有几个文本字段,用户输入信息并点击一个按钮,该按钮调用一个很好地执行POST任务的方法。 我可以看到服务器从客户端的POST表单接收数据,但我的问题是该信息(来自表单)去了哪里? 它应该创建一个新资源(在本例中是一个Person资源)...这是执行POST的PersonResource类的代码。

@POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Person postPerson(MultivaluedMap<String, String> personParams) { String firstName = personParams.getFirst(FIRST_NAME); String lastName = personParams.getFirst(LAST_NAME); String email = personParams.getFirst(EMAIL); System.out.println ("System storing: " + firstName + " " + lastName + " " + email); person.setFirstName(firstName); person.setLastName(lastName); person.setEmail(email); System.out.println ("person info: " + person.getFirstName() + " " + person.getLastName() + " " + person.getEmail() + " " + person.getId()); return person; }

它返回一个人员资源,但最终该人员资源在哪里? 如果我没有提供解决此问题所需的所有必要信息,我很抱歉。 如果需要更多信息,我将很乐意提供。 我非常感谢任何帮助。 谢谢。

I want to first say that I am a newbie to Android and java (to a lesser extent). I have a client-server application, the client is an Android app and the server is running Tomcat. Considering I am new at this I am a little confused about the POST request. The Client has a couple text fields, the user enters information and hits a button that calls on a method that well does a POST task. I can see that the server receives the data from the POST form in the client but my question is where does that information (from the form) go? It is supposed to create a new resource (in this case a Person resource)...Here is the code from the PersonResource class that does the POST.

@POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Person postPerson(MultivaluedMap<String, String> personParams) { String firstName = personParams.getFirst(FIRST_NAME); String lastName = personParams.getFirst(LAST_NAME); String email = personParams.getFirst(EMAIL); System.out.println ("System storing: " + firstName + " " + lastName + " " + email); person.setFirstName(firstName); person.setLastName(lastName); person.setEmail(email); System.out.println ("person info: " + person.getFirstName() + " " + person.getLastName() + " " + person.getEmail() + " " + person.getId()); return person; }

It returns a person resource but ultimately where does that person resource go? I am sorry if I have not provided all required information needed to solve this problem. If more information is needed I will be happy to provide it. I truly appreciate any help given. Thank you.

最满意答案

我不确定我是否完全理解这个问题,但这里有一个可能有用的概述:

客户端POST向服务器发送HTTP请求。 服务器必须具有某种处理请求的Web服务框架(例如Jersey或CXF或......)。 您的类(@POST和@Consume)上的JAX-RS注释指示Web服务框架将请求路由到您的类的postPerson方法。 这听起来很有效,是吗?

然后,您的方法基于表单的内容(即用户输入)构造Person对象。 您的方法将此Person返回到Web服务框架。

那么这个人会怎么样? 您提供的@Produces注释指示Web服务框架生成人员的JSON表示,并将其包含在发送回客户端的HTTP响应正文中。 响应可能如下所示:

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Content-Length: 259 {"fistName":"Bob", "lastName":"Barker", "email":"bob_bark@priceisright.com"}

这是你在寻找什么?

I am not sure I fully understand the question, but here is an overview that might be helpful:

The client POST sends an HTTP request to the server. The server must have some sort of web service framework (e.g. Jersey or CXF or ...) that processes the request. The JAX-RS annotations on your class (@POST and @Consume), instruct the web service framework to route the request to the postPerson method of your class. It sounds like this much is working, yes?

Your method then constructs a Person object based upon the contents of the form, i.e. the user input. Your method returns this Person to the web service framework.

So what happens to this person? The @Produces annotation you have provided, instructs the web service framework to generate a JSON representation of the person and include this in the body of the HTTP response that is sent back to the client. The response might look something like this:

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Content-Length: 259 {"fistName":"Bob", "lastName":"Barker", "email":"bob_bark@priceisright.com"}

Is this what you were looking for?

更多推荐

本文发布于:2023-08-04 16:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1418964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   RESTful   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!