简体   繁体   中英

Set up a server that connects to a database, obtains the information from it, then sends it to an Android application

I have this seemingly complicated project to do: I have an android application that has to connect to a server, and that server has to provide this application information from a database.

Now, I have to write this server from scratch, in Java (because that's the language that I know and I want to use). Here's my plan:

1) Create the database, populate it with information (from the MySQL terminal, for now). Eventually, this server that I'm creating actually has to connect to another server and obtain the information from there, and then populate the database with that information. But I'm not at that stage yet.

2) Create a server (I'm guessing, use Tomcat 7) in which a few classes are "running": one that connects to the database using JDBC to obtain the information from there and create a new object from each record it gets from there (not exactly sure how I do that); and another class that acts either as a JSON endpoint or as a sockets connection server (never did anything like this in my life - sure, I've connected to a JSON endpoint in the past, but I have never created a JSON endpoint myself, server-side). This connection has to be updated (new information from the database has to be sent from the server, or requested from the server) every 2 seconds or so, so it works in a real-time manner.

3) Create a way in my Android application to obtain the information from the server. If the solution at the server-side is a JSON endpoint, then obviously, connect to that JSON endpoint and obtain the information like that. If it's a sockets connection, apparently, the server should push the information on a certain port regardless if any client is connected or not to that port. Again, no idea how to do that exactly.

4) Then, in Android, parse the data and use it to be displayed on the screen, in lists etc (that's the later stage, once I get the data in my actual application, from the server).

What would you guys recommend? What should be my approach?

I suppose I should install some kind of server like Tomcat 7 and then... how do I actually "run" the classes that connect to the database and how do I create the JSON endpoint so my application can connect to it?

Obviously I will look through tutorials in the meantime, but how do I get these classes, assuming I'm building them, to actually RUN in Tomcat (meaning, to actually be visible on the IP of the machine that runs as the server)?

I know these are quite a few issues I'm presenting here, but... this is what I have to do. Any help will be appreciated.

Furthermore, do you see a sockets connection as a viable option for my application, instead of JSON? (although, in the Android world, JSON seems to be "the norm"). Remember, for now, I will have to have this information updated every two seconds. How do I maintain the connection open between my application and the server so that it doesn't do a request every time or anything like that?

Again, thanks for any help!

You are talking about a typical Client-Server communication. There are several paradigms to achieve this and various ways to go about it.

1) MySQL for DB is a good and widely used choice.

2) You can use Apache Tomcat, but the kind of server you would need also depends on the scale of your project(along with a lot of other things). Tomcat works typically and looking at some basic aspects of what you have specified, it should be enough. Other options are JBoss, GlassFish, etc and they are not as lightweight as Tomcat and target a different class of applications. For the endpoints from server and client. You have several technologies - Webservices, RPCs, EJBs and more. I would go with web services. REST based ones as I feel they are simpler. There are plenty of libraries available and good tutorials to build them. Database - Server interaction, if you would require transactions and object to relational model mapping, consider an ORM tool. Hibernate is widely used.

3) Depends on what technology you use in 2.

4) Not sure on the client libraries that can be used in Android. Somebody else will have to fill in on that one.

Hope it helps.

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