File upload from android to server with socket.io nodejs - android

I have integrated socket.io into my android application to make a chat application. Now I wants to upload and share files like images, mp3 or doc file etc. How do I upload a file with socket.io I cannot find any documentation for doing.

It is not a good practice. This may lead blocking heartbeat messages between client and server if upload progress is long.
You may upload file with http post and send download link to chat peer. By doing this, a new connection is established for uploading and never breaks socket.io session.

I believe it should be a good practice to do in Android, as it works pretty well on web JS for small files such as Images, PDFs, etc.
But ya sending the whole file in a single shot is not a good practice(theoretically there should be no problem in doing that using socket.io), but it's better if you can the fragment the file into smaller pieces (maybe 2^10 to 2^20 bytes), and send it in pieces, doing so will also allow you to know the percentage of file's data transferred.
I am not good with JAVA, but I hope you can find a way to do that fragmentation thing.
But there is no inbuilt resource available in socket.io to transfer files, but you won't have any problem in sending pure Buffer(including '\0' in it) if you can manage to create a JSON object in JAVA without involving strings. As '\0' inside Strings messes them up.

Related

What is the approach for list / stream / download files from cloud?

On an Android device, I'm trying to list down mp3 file names (list view) , stream/buffer a mp3 file , download the mp3 file available in the cloud.
I was thinking about couple of approaches that I can follow to get this done
Host mp3 files on a server, & probably create a web service which gives me the above functionality and at the client side, I should be consuming the web service. ( Single server for both Web service & hosting mp3 files ).
Host Mp3 files on a server1, Deploy a web service on server2, this way I can easily structure and manage mp3 files.
What is the usual approach that app's like Google Play Music, Saavn ... follow ? If there is any other solutions I can look into, please suggest them.
It would depend on the amount of space your data will occupy, the bandwidth available on your server infrastructure, the kind of speed you need, how fault tolerant you want it to be, the budget you have etc and many more factors.
Generally, I'd suggest you use a single server to host your data and application logic in the beginning. If/when you start getting more users for your service, then you can start thinking about scaling up using a more distributed architecture (like maybe firing up a couple of instances of Amazon S3) etc.
Your client mobile app should be implemented in a way that it doesn't need to know what kind of backend architecture is supporting it so that you can easily change the architecture later without affecting the client side code. Look into RESTful web services for this.

how to download PDF file from server.

I am trying to download a file from server, what I have in mind to get filename in json response and the content and write same in assets or on sdcard. should i go and implement same, or there can be some other way we can achieve the same.
Actually you know you control the way you expose the content from the server - you can just make the file itself (its bytes) exposed at certain url. If you need to keep the name of the file posting as json seems to be good solution.
However, there is one trick here: I don't know how big the file you refer to is and also how reliable the network will be. For every bigger file I will recommend to implement resumable upload via byte serving. You can read about the byte serving client consumer in Android in this thread. Then just go and see on how to implement byte serving server side - it definitely depends on the platform in the only case in which I used such mechanism I actually had to mimic the byte-serving with my custom url parameter. If you need further help on server side write back and I will extend my answer.

Upload file for primitive chat

I need a fast way to upload files to a ftp server.The files will be used for a primitive chat.Something like a timer and every 5 sec checks to see if the file x was added,if it was added open it and show it.
So please tell me an easy way to upload a file,because from google,I got only hundreds of lines of code.And yes,I need it for a FTP server.
Also it would be awesome to tell me a way to acces a file trough ftp open it and show it.(don't really need to download).
You should use the org.apache.commons.net.ftp.FTPClient library - it's free and implements fairly easily with Android. You can find a pretty easy how-to at the bottom of this blog post:
http://hoang17.com/posts/android/android-how-to-upload-a-file-via-ftp
That article should be especially helpful as it shows you how to store a string as data in a specific filename.
To be honest though - FTP seems like the absolute worst way to create any form of chat communication. :)
Cheers.

OutOfMemoryException in kSOAP

I am sending multiple PDF files through SOAP which will be received by an Android client. But when receiving the SOAP response, it is throwing OutOfMemoryException.
I want to know whether its a limitation of kSOAP or Android. Please guide me how to overcome it.
Thanks.
I would suggest to not do that. Just put the raw url to the pdf file into the soap message and download the pdf separately independent of soap. That works great for me with PDF files as well as images..
So in a bit more detail:
One of your results from the SOAP request should contain a full public url to the PDF file somewhere on the internet.
Then use DownloadManager or whatever you want in terms of Android development to get the file downloaded by using the url you got from the soap response. But dont have the PDF wrapped within the soap request. That way you can also show a progress bar during download and so on nicely. Downloading files on Android is documented everywhere..
I now that this is old question but maybe I help other developers. The most efficient way to sending a large binary content from/to web service is MTOM transfer (SOAP with attachments). The problem is that ksoap2 library doesn't support this feature, but you can try http://easywsdl.com generator. What I know is that it supports MTOM transfer and allows you to send/retrieve very large binary files. Of course MTOM transfer has to be enabled also on the web service side.

How to download only the first part of a .csv file on Android?

In my app, I have to present a few numbers from a .csv file that's accessible from the web. Now, this .csv is quite big. I don't want to download and process the whole thing, there's no point. My numbers are always in the beginning of the file, in well specified positions - lets say position 5 to 10.
Could you give me some tips on how to implement this? I know how to download the whole thing, but don't know how to download only a part of it.
Thanks.
Psuedo:
BufferedReader br = new BufferedReader (new InputStreamReader(remoteStream));
String sFirstLine = br.readLine();
remoteStream is the stream of the connection to the remote server. Getting a handle to the stream is not the same as actually downloading. Only the BufferedReader.readLine() actually downloads anything
Do you have access to the server the file is on? You could do your processing on the server.
For example, if the file is at /myfile.csv, you could open a stream to mycsvfile.php on the server, which does the processing and returns the positions you're interested in. Of course you'd also have to write the PHP code (or whatever) to do this.
You don't say what protocol the file is served under, but assuming something standard like an http or ftp there should be nothing to stop you from starting the download and then aborting it once you've gotten as much of the file as you need, provided that you implement the android end of the protocol yourself rather than using one of the built in mechanisms (unless you find the built in mechanism also gives you the ability to abort).
I don't believe the implementation of a simple http downloader in java or ndk to be too complicated. Doing it on android should not be uniquely challenging as you have all of the normal java and underlying linux network sockets mechanisms readily available.

Categories

Resources