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.
Related
I am creating an authentication system where I get some value( let us say "x") after processing the android application. I want to send the value "x" to the server where I have a java file and it needs to be run on the server end(may be using php by passing the argument as the value "x"). After running, I need the output to be sent back to the android device to display it.
Leads how to implement such short structure will help me complete the system.
You need to create a class that extends AsyncTask, and then use your php source that u would normally use for login. Here's a link that might help you out: Android, PHP authentication
Try using HTML request. I am not sure how is this implemented. Any one can comment regarding it.
I spent some time searching for a way to send data from a android application to Matlab - with no approach. I would prefer to do it with JSON via a Restful webservice.
I probably have the wrong concept in mind how this is going to work.
Matlab should be running/waiting for POST requests from my android device to receive the data, bring it into matlab form from json , progress it and send it back - than wait again for new requests.
The "RESTful web service" like "webread" seems not to wait for incoming data and go active for them.
How to let Matlab listen for incoming data with json ?
or how to let Matlab receive data from Android/java based programms ?
Do i need another frameworks, api's or even a server with Database to get that done ?
Can anyone give me some hints ?
Approach 1:
Matlab also provides Matlab Mobile https://de.mathworks.com/products/matlab-mobile.html, which is capable of executing Matlab code from your device, however, it is not possible to send images to Matlab.
However, you can use WebCam https://play.google.com/store/apps/details?id=com.pas.webcam&hl=en and open up a Server, which is pretty straightforward. You can run the app in the background and then connect to Matlab via Matlab-Mobile, and access it via your IP address and usually Port 8080.
Approach 2:
You can use a WebSocket -Server which is implemented here:
https://de.mathworks.com/matlabcentral/fileexchange/50040-jebej-matlabwebsocket
For more information on how to get it to run you can follow the directions given on the GitHub readme, here: https://github.com/jebej/MatlabWebSocket
A WebSocket Server is on the highest layer of the 7th layer (application layer) of the OSI model https://en.wikipedia.org/wiki/OSI_model and builds op on the 4th layer (TCP). However, you do not need to specify such things as buffer size etc.
The following example code is directly taken from the example code from the GitHub project. To fulfill the desired outcome in the Android application it is the best approach to rebuild the client application on Android.
Echo Server:
classdef EchoServer < WebSocketServer
%ECHOSERVER Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function obj = EchoServer(varargin)
%Constructor
obj#WebSocketServer(varargin{:});
end
end
methods (Access = protected)
function onOpen(obj,conn,message)
fprintf('%s\n',message)
end
function onTextMessage(obj,conn,message)
% This function sends an echo back to the client
conn.send(message); % Echo
end
function onBinaryMessage(obj,conn,bytearray)
% This function sends an echo back to the client
conn.send(bytearray); % Echo
end
function onError(obj,conn,message)
fprintf('%s\n',message)
end
function onClose(obj,conn,message)
fprintf('%s\n',message)
end
end
end
To run it in MATLAB type:
s = EchoServer(30000);
This will then utilize the port 30000 on your local machine.
On Android simply create a WebSocket-Client and use your URI, which you can find out by using ipconfig (windows) or ifconig (Linux). In Android the uri should like the following:
ws://192.168.1.102:30000
Where the IP address may change according to your IP address
Here are my 2 cents.
Your approach seems right.
Step 1: You need to run a web server using MATLAB on your device. By going through Web Server, it looks like you can use it to run webserver, and execute a .m file when a POST or GET request is made to your server.
Step 2: Lets say that your server is accepting requests on port 8080. From your Android device, if you are on same network then you can make a HTTP POST request to http://your.ip.address:8080 and extract the data and execute your code in .m file.
Note: You can also get a public URL for your local server running on the device using ngrok utility. Then make a POST request to that public URL. You need not be on the same network then in order to make a request. Here is some explanation: Accessing localhost from android over wifi.
Edit:
Additional question says:
Matlab is possible of receiving data via TCP/IP client, but how does the android site need to do the POST/GET algorithm and how can response Matlab responsible to it?
Let me rephrase what I understand. Firstly, you want to know how from Android code, one can do a POST/GET request and secondly how will Matlab respond to the request?
In Android you can make a POST request in background thread either using AsyncTask (Android HttpURLConnection with AsyncTask Tutorial) or if you want to do it properly, you can use Retrofit library to do POST/GET calls (Using Retrofit 2.x as REST client - Tutorial
).
When using WebServer as mentioned in the link previously, when the .m file gets executed on the POST call, you can send the response to the POST request from there. On Android, where you initiated the call, you can receive the callback.
Hope this helps a bit.
Have you tried the Android Support Package for MATLAB?
While it doesn't allow access to the camera, when used with MATLAB Mobile it does provide access to:
Acceleration on 3-axes
Magnetic field on 3-axes
Angular velocity on 3-axes
Azimuth, roll, pitch
Latitude, longitude, altitude, horizontal accuracy, speed, and course
Here's a link with more detailed information on how to get started.
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
I have a website that is a companion app for a java application. Namely, the web app fetches information from the java app and inputs it into my database and lets the user access this information. Right now I have the system set up so the Java app posts to the website's php files when I tell the Java app to.
However, I am wanting to set the system up so that the java app posts to the website when a command is issued from the website, such as a refresh command. Is this possible?
This is extremely vague. You should not think of the problem in terms of "posting from PHP to Java", in fact every HTTP request is simply that - an HTTP request. PHP and Java both offer several ways to respond to HTTP requests. It's up to you to compose the scripts that will respond to each request. Think of this in terms of the requests you need to make and the responses they should receive, then focus on writing a script for each of those interactions.
You can call Java directly from PHP. Zend Server and PHP/Java bridge both enable this. Depending on your requirements one of those may be appropriate.
Alternatively you can make use of message oriented middleware. This requires you to install and run a message broker such as RabbitMQ or ActiveMQ. However client libraries are available for much more than just PHP and Java.
Both of these would be better alternatives than relying on HTTP requests.
We have a php setup for our web pages that is secure with HTTPS. The web app talks to a DB but we also want it to talk to a java server we have.
The java server is a standalone java application (not web). We simply want a callback action after the PHP page finished writing to the DB done in the java server. What is a good way for this php page to talk to the java program to get something done?
I usually recommend against quick and dirty but here :
You can dump data in a file if it can be asynchronous. Then a cron job from java, checking for that kind of file at a regular interval, do the specified command.
For example, you can dump the word ExecuteCmd1 in a file. The java thread reads it, interprets it and choose that he must execute the method or class with the same name.
You can do the same thing over to go back to php.
Probably via a TCP/IP connection. If your Java application runs a server, then the PHP script can connect and send a message informing the Java app that the DB has been written to.
Do a quick and dirty JSON RPC from PHP to Java. You could probably get it up and running in one cup of coffee.
Use CURL on php (http://php.net/curl) and json_encode() to POST a json string to your Java server. (scroll down and find the curl wrapper class that someone wrote in the comments. It's easy.)
Use JSON (http://www.json.org/java/) in Java to decode it and use it immediately. Send your response back in JSON too.
I had a similar XML RPC system running in production for years. PHP -> IP -> Java works great.
Google Protocol Buffers Not so much dirty, but works, and works well, regardless of which launguage you use.
You can try the PHP/Java bridge. I used it a while ago to use Java logic inside Typo3, a PHP CMS.
My advice, whether you use the bridge or not: make sure you know where the errors come from if something doesn't work. Check both PHP and Java logs. Be verbose if an exception occurs.
How much data do you need to transfer?
How many requests per second?
Does the Java application have to handle the request immediately, or is it enough to handle the request in a few minutes?
Does the Java application need to return data to the user's browser?
If the answers to questions 3 and 4 are no and no, you could just create a database table for the jobs, have the PHP app insert a new job, and have the Java app poll the job table every minute or so.