简体   繁体   中英

Java server to handle requests from iOS via JSON?

I've been tasked with creating a server which will receive requests for information from an iOS device (iPhone) and then return results based on this request. I've done some research, and it seems that JSON is the way to go for this task. The thing is that I have no idea where to start; how my server should be put together, how it will handle requests etc. Can anyone point me in the right direction and perhaps suggest some reading?

If you are comfortable with Java, I would recommend getting the Eclipse JavaEE version, and then: Create a new "Dynamic Web Project". File/New/Other/Web/Dynamic Web Project

Then, in your new project, create a "New Servlet". File/New/Other/Web/Servlet

This should create a skeleton server, with a doGet and/or doPost method. You can debug the servlet right away with Eclipse; it helps you get everything set up (comes with Jetty built in, these days)

You can be set up with your first webserver in 20 minutes.

Grab the JSON jar from json.org. Drop it in WebContent/WEB-INF/lib inside your project.

In the doPost of your new servlet, call request.getParameter(...) with the name of your post param that has the json in it. Pass that to the JSON parser.

To pass JSON back, get the JSON string from a JSONObject, and use response.getWriter().append(...)

Ta-da!

Pretty brief answer, but then again, it's a pretty open question.

If you want to build it in Java and want to use what all the cool cats are using, Dropwizard is the way to go. It's geek factor has increased after its last photo session for Vogue (either that or the newest edition of Thoughtworks technology radar ). No, really - it's good and extremely easy to deploy and monitor.

If you just want to get it done and over with, a short PHP script is the way to go.

WARINING: If you don't know PHP, it's fine: you can still use it. If you don't know Java, stay away from it . Learning Java on the go is a big mistake.

You can get started fast using Spring Roo. It can automatically create controllers for your domain objects that return JSON data for Restful requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM