REST server and video streaming - java

I am building a restful web server with jersey in conjunction with an android app. One of the thing that the server needs to do is to save a video uploaded from an android client and be able to see that video in streaming on other android devices using my app.
I wrote this little test function:
#GET
#Path("video")
#Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response video() {
File file = new File("C:/Data/video.mp4");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.build();
}
and seems to randomly work. sometimes the client just loads for ages and i need to close the app. when i close i get this exceptions:
An I/O error has occurred while writing a response message entity to the container output stream.
that error makes me think that when the connection is pretty slow like with 3g instead of wiki, the load takes a lot of time and when i close the app the connection closes so the server can't write on it. so here is my doubt:
1) streams should be used with UDP but udp doesn't have handshaking so that type of error makes no sense to me. does it means i am using tcp?
2) im i actually sending a multipart file? because it seems like it just download the entire file and then start the play.
my final thought is: is it worth to do this with rest? there is any external service that can store and stream the file for me, maybe adding some service like compression and stuff like that? and if not how should i do this? can u guyz show me an example?
ty for the patience and sorry for my english :D

Related

How to make Android app wait for a message from Python on a local server without internet?

Sorry if this is a silly question, I'm still a beginner in Android and couldn't find an answer to my question. I'm making an Android application that sends an input to a server and executes a Python script on that server to process the given input and generates an output. I was successful in sending data from Android client to Python server using SSH.
I can also use SSH to retrieve the output back to the Android client. However, the Python script takes some time to generate the output, and I can't seem to find a way for the Android client to wait for the Python script to finish generating the output. I was able to do this on internet connection with using Firebase database and have the Python script upload the output into Firebase database and have the Android client listen for changes in database. But I'm looking for a way to do this locally without internet (i.e. Firebase).
So is there a way to make my Android application wait for a message from Python to know it has finished with generating the output so it can retrieve it back using SSH or any other way?
Was a protocol what was send a code what was suppost to say if that 2 files are the same. but i do not remember the name. For u will be more easy to send the storage information's in bits (to the device) and after to start that important transfer. when the device will lost the connection will need to compare the real storage memory with the first information's send it and if has not match to ask the user to reconnect to internet or something like that. Maybe is helping until some one will give u the answer.

How to send message from backend(java) to frontend(react)

Hi Im still learning how to use react and java. I am creating a project where the front end is in react and the backend is in java.
I was wondering if someone would be able to point me in the right direction or give me an example of java sending a message to the front end in react. What Im essentially trying to do is give the user a message after the backend receives a file they submit. I already have the part where the backend is processing the file.
Ive tried looking at a lot of sites but theres not a lot of documentation out there for react and java as the backend.
thank you
What you need is Websocket, he provide a full-duplex communication channels over a single TCP connection. So, when you start a comunication between client and server, a session is created, making possible to send and receive message for both side.
Some possibilities are, use javax:
https://docs.oracle.com/javaee/7/api/javax/websocket/package-summary.html
https://www.baeldung.com/java-websockets
Use spring-websocket(if are you already using spring, will be easy to use):
https://spring.io/guides/gs/messaging-stomp-websocket/
https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/websocket.html
Here is another example with javax https://www.pegaxchange.com/2018/01/28/websocket-server-java/
So, there are a lot of examples.
Websocket would open a channel between the client and the server, so you can send a server message at any time.
But, if you are already using a POST from the client to upload the file, you could just send a response to the POST.

How to upload videos to JWplayer server from android programmatically

I am using JW player in my Android application. Is there any way to upload videos from my client side(Android app) to JWplayer Server? I gone through
your site https://developer.jwplayer.com/jw-platform/docs/developer-guide/management-api/uploading-videos/ to upload videos but its not clear that the code given is to upload the videos from android app or from a intermediate server.
Any clarifications on this would be greatly appreciated.
What we tried from our android app side is to upload the videos using below,
POST URL : http://upload.jwplatform.com/v1/videos/upload?api_format=json&key=vtQmcboj&token=e2bbad0fd889d5d2e30047596cfe3789778257d2
The params i sent,
api_key
api_secret
local_video_path
You cannot upload videos directly to JW player since its a Two tier process.
I have been workign in a java application where the user can upload videos, watch it and download it.
You will have to send the key and the token for the auth request, which JWplayer thinks is not safe from their end so they just will not allow you to upload the videos to their server.
To acheive the scenario, you must be running your own server which instead upload the video to the JW platform. All the error responses will have to be handled in your server side if the upload gets failed.
Also, JW player handles resumable upload process. So, once you sent the data to your server, your server can send the data in small units and the interruption in connection will be handled in JWplayer end.
In my own scenario, my server will be handling the resumable uploads from client(since its not possible to resume uploads from client without having valid protocols embedded in Server side) and my it indeeds send the data's to the JW player for encoding.
To be more precise,
Client ----> Your server ----> JW player server.
The POST method URI which you tried is for server side coding and not meant for client side. You can refer their support team for more details, since i came to know these details after reaching out them.
As #DeadSpider answered,
It is not possible from client side. Go for a server and once you get it work, cheers!
Yesterday i got a demo project from JWplayer and it is absolutely possible now to upload videos to your dashboad from client side.
http://qa.jwplayer.com.s3.amazonaws.com/~george/BishopQA.zip
Check the above project and in upload activity you will have the code. Its working for both android and ios platforms.

How to use a URI in java

Application use: I am creating a connection between a tablet and a desktop. This tablet will send command to my desktop that my java app will then interpret and do accordingly. One of these features I would like to include is controlling Skype. Such as focusing on it. Switching chats. Initiating call. Sending message. ect. But I can't seem get the URI api to work. (This is the only known api that will allow me to initiate calls and chats, so if you know of any other. Please do tell.)
I am not sure If I am doing it right being that this is my first time using a URI ever. This is what I have using
import java.net.URI
public void uriTest(){
try{
URI uri = URI.create("skype:echo123?call");
}catch(Exception ex){
ex.printStackTrace();
}
}
What am I missing? I know it is probably a lot. Is there some sort of way to implement it. Or when you call .create() does it automatically do it for you?
Any help or clarification will be greatly appreciated.
Your code constucted an URI and now you have one, nothing more, nothing less. An URI is - as the name "Universsal Resource Identifier" says - just an identifier.
What you probably want to do is establish a connection to a location identified by that URI. So you need a special identifier, a so-called "Universal Resource Locator" or URL:
URL url = new URL("skype:echo123?call");
Still nothing happening since we only defined a location. Next you have to connect to it:
URLConnection conn = url.openConnection();
Now you got an URLConnection-object (in this case a HttpURLConnection) and you can operate with it: post data, read responses etc. Read up on the API of java.net.HttpURLConnection to learn more.
You should write a web application, and deploy it on a web server program such as Tomcat, on your desktop machine. You can then write an app on your tablet which would connect to the server and send commands to the server.
The web application could parse the commands and do something with Skype application running on the desktop.
You would probably write a JSON api and deploy it on the Tomcat server, and then invoke the JSON api from the tablet app.

Client-Server Networking Between PHP Client and Java Server

I have a university project which is already 99% completed. It consists of two parts-website (PHP) and desktop (Java).
People have their accounts on the website and they wish to query different information regarding their accounts. They send an SMS which is received by desktop application which queries database of website (MySQL) and sends the reply accordingly. This part is working superbly. The problem is that some times website wishes to instruct the desktop application to send a specific SMS to a particular number. Apparently there seems no way other than putting all the load to the DB server... This is how I made it work. Website puts SMS jobs in a specific table. Java application polls this table again and again and if it finds a job, it executes it. Even this part is working correctly but unfortunately it is not acceptable by my university to poll the DB like this. :(
The other approach I could think of is to use client-server one. I tried making Java server and its PHP client. So that whenever an SMS is to be sent, the website opens a socket connection to desktop application and sends two strings (cell # and SMS message). Unfortunately I am unable to do this. I was successfully to make a Java server which works fine when connected by a Java client, similarly my PHP client connects correctly to a PHP server, but when I try to cross them, they start hating each other... PHP shows no error but Java gives StreamCorruptedException when it tries to read header of input stream.
Could someone please tell what I can try to make PHP client and Java server work together? Or if the said purpose can be achieved by another means, how?
Regards,
Yasir
Wait... are you using object streams? According to the java documentation StreamCorruptedException is "Thrown when control information that was read from an object stream violates internal consistency checks." I doubt your PHP app is sending what Java considers a serialized object. Why don't you go low-tech and read a string? The following had worked for me back in the day:
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while((inputLine = in.readLine())!=null)
{
//Do whatever
}
You might try looking into Quercus. It's a server that runs PHP inside java. You can call java called directly from PHP as if it was native PHP functions. You won't have to worry about streams then.

Categories

Resources