RESTful/JAX-RS with Swift? - java

I have built an Android application, which exchanges data with a server. For this purpose, there is a RESTful web service running on the server, written in Java with JAX-RS. The tricky thing is: I do exchange the data between application and server via Streams (Object- or FileOutputStream for example).
Now I want to build the same application for iOS, using Swift. Is there a way that I can read those Streams with Swift? And it is of course not only the reading of the Stream. As I do send Java Object, for example a String, can I receive this Object in Swift? I think it is not possible and I have to get rid of all that Streaming stuff and use Media.Types. But maybe I do have a chance to leave it like that?
Any ideas?

I think you are overcomplicating yourself a lot. The two apps should never have anything to do with each other. No matter what ecosystem they're running on (whether iOS, Android, Windows, etc) they shouldn't have to be aware of each other at all.
The same applies to the service. It shouldn't be concerned about which client is being consumed by. It should only care about receiving a "client-agnostic" request and serve the response. Now, the service would be SOAP/XML, plain json text, whatever. It's the client's responsibility to produce the correct request and understand the response from the server.
So, to answer your question...
Is there a way that I can read those Streams with Swift? And it is of course not only the reading of the Stream. As I do send Java Object, for example a String, can I receive this Object in Swift?
No, you're iOS app shouldn't have to deal with java code, it should only need to make the correct request to the server and understand the response...i.e. by making a service request using a NSURLConnection and parsing the response
So, just focus on making the service endpoint(s) platform-agnostic so that any client can consume it

Related

Sending and receiving data from an Android application to a web hosted Java application

I've been looking around all morning, and can't seem to figure this one out. I know it's not as complicated as I think it is, and all I need is just some pointers to the right direction.
I have an Android application that takes some user input, sends it to a Java application over the web, and then receives some output based on the input.
My Android and Java applications are ready, but how do I go about sending and receiving the data over the web? I understand that I'll be sending and receiving HTTP requests, but my problem is where I'll be sending them and receiving them from. Do I host my Java application on a Servlet like Tomcat, or do I use something like OpenShift to host my application for me?
I have no trouble with sending some HTTP request from my Android application to the web and receiving some output back, but my problem is that my web service needs to use a Java library to process the input and generate the output. I'm just not sure how I'm supposed to get this data to and from this Java library that needs to be hosted on the web.
I hope my question makes any sense.
EDIT: Perhaps I wasn't clear with what exactly I need help with. I do realize that the architecture I use doesn't really make any difference, but the problem I have is with how I'm supposed to use this architecture.
Let's say I have a registered domain name that I can easily send to and receive data from using my Android application - no trouble with that. But how am I supposed to integrate my Java library with this server? Do I just create an applet and put it on my server? Does my web host even allow Java applets to run?
I guess my question is how I'm supposed to get through the "barrier" between my HTTP request / transport layer protocol and my Java application.
Server architecture usually does not matter. You can use Java, NodeJs, Rails, Python, .NET, etc. You just need an endpoint that accepts a HTTP POST/PUT/GET/DELETE/PATCH verb.
This is more a question of "how do I set up a server to accept input" and its a very large topic in itself. I would advise you look at PAAS solutions like:
Parse.com
Kinvey.com
You can use their tools to build a solution fairly quickly. If you need something custom you'll need to build your backend in the language of choice and host it online via AWS, Google Cloud, Heroku, a VPS or something similar.

Is it possible to add PHP scripts in a Java server?

I have a java server running with the spring framework. For now I have to develop a GCM service to send data from the server to an Android app.
I have been seeing some tutorial on Internet and all of them implements this service in a php script. I'm wondering if I can use them in my java server with spring.
I know I can rewrite the code to Java but I don't have so much time as it's a work for University, so I don't wanna waste much time rewriting all of this..
So I'd like to know if it is possible to add these php scripts and if so, how can I do it?
Even if you interface your Java server with some PHP code that sends GCM messages, you would still have to pass registration IDs and notification payload to that code. Wouldn't it be easier to simply use the gcm-server.jar supplied by Google?
Using gcm-server.jar, the code you need to send a GCM message is as simple as this :
Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
.delayWhileIdle(true)
.addData("key1", "value1")
.addData("key2", "value2")
.build();
Result result = sender.send(message, registrationId, numOfRetries);
If you wish to handle error responses, you should check the result instance to see if the message was successfully received by the GCM server, and if not, what error occurred.
The only other thing you'll have to implement is some servlet that accepts registration IDs from your Android app and stores them in your DB. Perhaps you can use existing PHP code for that part (you won't need any interface between the PHP code and Java code).
you can execute external programs from your java code
you will have to pass the data back and forth somehow. reading/writing stdin and stdout probably
maby this will help: Execute external program in java
Yes, you can. They have interpreters for PHP, JavaScript, and even Python written in pure Java. For PHP, one such offering is Caucho Quercus.

How can i Get back source data of data contained in a script PHP with Java

I explain, I tried to open my database .sqlite contained in my server in Java with jdbc:sqlite:mydb, but it doesn't work because my database ".sqlite" is distant so i creat a script php based on my server which gets back me all of my databases.
So my question How can i Get back source data of data contained in a script PHP with Java?
Thanks a lot!
PS : Sorry for my english (I'm french)
One way would be to create a service in PHP which offers the data, so that your Java application can consume it.
E.g. PHP can expose the data in an XML or JSON format, for instance over HTTP. Your Java application would then call the HTTP URL and receive the data in JSON/XML/whatever, and then parse it to extract the data from that format.
There are other ways as well, e.g. a service bus could broker the data between the different systems, but that requires much more setup (though in the long run, it does have value, if you keep adding data that you want to expose to more and more systems).
You can create PHP WebService server and write some functions to operate that databases data for fixing your problems. Then just use Java to connect with soap to your created WebService functions.
How to start webservice, try to read this one:
http://greatgandhi.wordpress.com/2009/11/25/create-a-php-webservice-in-5min-using-php-soap-and-wsdl-technology-nusoap/
Its the best way i know, if you need to send some data back to your remote db. Its a little more complicated, but much more correct way to operate remote data.

What to I need to do to on my server to communicate with an Android app?

I am currently writing and Android app, and I need to do some stuff with a server. Actually I just don't know where to begin, since I don't know very much about server side.
My Android app will send Java objects and images to my server, so what do I need to write or use on my server ? Is it possible to write Java code instead of PHP on the server side ?
Could you please give me the steps to prepare my server ?
Is it possible to write Java code instead of PHP on the server side ?
You can use any programming language you like for server side programming. This question provides some useful information on various Java based approaches using the HTTP protocol.
will send Java objects
You will need to serialise them to a data format. JSON and XML are popular for transmitting structured data across HTTP.
and images to my server
HTTP allows the transmission of files and Java can construct the request.
I see that you just want to communicate between the server and client(Android). I would recommend considering REST style web service for your server, where you can use either JSON (recommended) or XML format.
How to implement REST in a web application?

Android: Transfer file over TCP Java Socket

I am currently trying to transfer a file from a Android device to a Java TCP Server, but I am unable to find a good example which explains the structure I would need to implement this. There are many Java Client&Server examples there which explain file transfer but I want to make sure if this will still work once one throws an Android Device in there.
My question is how do I implement this sort of structure? And if it doesn't work, would I be better sending the file over an HTTP connection to a PHP server? I see a lot of examples and documentation online for the later method so I presume it is more reliable. I would however prefer to use a Java server.
The file consists of a large set of coordinates recorded by the Android device which will then be sent to the server. I have not yet established how I will store this data yet but I was originally going to store them in a primitive text file.
Design
The first thing you need is something to allow you to run Java code on your server.
There are a number of options. Two of the most popular technologies are Glassfish and Apache Tomcat.
Crudely speaking Apache Tomcat is sufficient for simple client-server communication and Glassfish is used if you need to do more complex stuff. Both allow Servlets (which are essentially self contained server classes written in Java) to run on the server-side.
They handle communication with the client by launching a JVM (Java Virtual Machine) each time they receive a request. The Java servlet can run inside the JVM and respond do some processing if required before sending a response back to the client.Each new request is run in a new instance of a servlet. This makes dealing with multiple concurrent requests simpler (no need for more complex threading).
Networking (sending data to and from the server)
In networking situations the client can be a PC, an Android phone, or any other device capable of connecting to the internet. As far as the server is concerned, if the client can communicate using HTTP (a standard protocol which it understands) the it doesn't care what sort of device it is. This means that solutions for PC desktop client-server applications are similar to one for a phone.
You can use library such as Apache HTTP Components to make it easier to handle HTTP requests and responses between the device and the server. Of course you could write your own classes to do this using Sockets but this would be very time consuming, particularly if you have never done it before.
Storage of Data
If you have time I would recommend implementing some sort of database to store the information.
They have a number of benefits to such as data recovery mechanisms, indexing for fast searching of data, ensure data integrity, better structuring of data and so on.
If you decide to use a database I recommend MySQL. It is a free and more importantly - well documented.
Aside: JDBC can be used to communicate with the database with Java.
Sorry about the in-line hyperlinks - apparently my repuation isn't high enough to post more than two!
Source: Personal experience from implementing a similar design.

Categories