I was wondering what the preferred way of sending an image to a webserver from an android application?
I've seen people
opening up sockets and send the actual image data.
base64 encoding it and sending it as a string.
What is the preferred way? Can someone post some examples as well.
Thanks
You could try writing it to an xml file as a base64 string then send it an parse the file on the server and decode the string to an image.
An HTTP Post is probably the easiest way, sending it as multi-part form data. Makes it easier to receive as well (a bog standard web server can handle it!)
Related
I am going to write a Java program that does a HTML "get"-request. The response is a MP3 file.
The question is: Which Java class/methods can be used for doing this and how can the MP3 file received and stored in a folder ?
(Background: Desktop is connected to teh Internet, Java program is running on Desktop and send a request to an URL that responses via a content-type audio/mpeg. But how can I receive this mp3 file ?)
Thanks and best regards
Harald
The simplest approach is to use a java.net.URL and its openStream method to read the data bytes and write it to a file using a java.io.FileOutputStream.
I have the following URL
http:actionname.php?user=username&password=pwd&sender=no&audio_file=somefilehere
I want to send an audio file along with the above parameters.
Is it possible to send an audio file as a request parameter?
Thanks in advance...
You can send the name of a file, but not the file itself. URL lengths are limited, practically to about 2000 characters. Theoretically they may be a bit longer, but this is not supported on all browsers. See this answer.
If the file is publicly available, and the file name is a URL itself, the server may fetch the file later.
If you want to send a file, consider having a POST request with the file content in it.
As usually, you will send anything as request to web server, the file content will be transferred as internals of HTTP message (POST parameters as file name, and content separately) outgoing from you to the server, until it finished (it is TCP session and it won't be break up while data is streaming), it could be considered as "request" to web server.
Theoretically, you could send something via URL (HTTP GET request), but you could develop some kind of transport data-over-HTTP-GET protocol to send escaped binary data chunks as GET parameters. It is possible :)
Even sending (uploading) file via POST request also no so plain as submitting form with usual POST parameters, you should use multipart/form-data encoding in the form and special treatment on the server side for that.
It is not good, but not so weird as transmitting data via URL, you could send some data within GET request to the server, look here for more.
I have gone thru javadocs of URLEncoder and URLDecoder. Then got more inquisitive. Consider the server as tomcat.In any webapplication whenever we submit
the form , server converts the forms fields into urlencoded fields and when we do request.getParamter("fieldName"). Server again decode it with URLDecoder.
Is that correct? Does server do it or browser? Simlary when we type any url in address bar same stuff happens? If server or browser does that
when we require to encode or decode the request paramter explicitly with the help of URLEncoder and URLDecoder? Though these are basic questions but could not find these anwers clearly.
In any webapplication whenever we submit the form , server converts the forms fields into urlencoded fields and when we do request.getParamter("fieldName").
No. The browser does that.
Server again decode it with URLDecoder. Is that correct?
Yes.
Does server do it or browser?
The browser.
If server or browser does that when we require to encode or decode the request parameter
explicitly with the help of URLEncoder and URLDecoder?'
I don't know what that means but it's still the browser. You only need to encode the request parameters if you are sending the request from application code. You don't need to decode them at all if you're running in a servlet container: it will do that for you.
While it is true that browser does encode a URL before passing it off to the web server but there may not be a browser involvement all the time.
e.g. your server app might be making a REST based call and passing some data in a simple GET request. And then if you don't encode it on your server it may become garbled when receiver decodes it.
Therefore it is highly recommended to always encode the URL before sending it off in your server code.
I have an application made in Java for Android platforms, and I need to send quite a lot of data, the data I am sending is the previous weeks SMS messages received and sent. That includes the following data:
Sent by Phone number
Sent by Name
Sent to Phone number
Sent to Name
Time sent
Message
And you can imagine this data can be really big for some people so I need a way to minimize/encrypt it on one side, and then using an HTTP request I can send it over to my server, and decrypt this data with PHP. What is the best method to do this? What size limit does HTTP POST have?
I think you should zip+encrypt(say, using aes) the data on Android side before sending the data to your server and decrypt+unzip it on the server side.
Post size depends on the config on the server side -> What is the size limit of a post request?.
p.s. why would anyone want to send all their sms messages to your server? :)
You'll want to send the data using an HTTP POST. The limit on the HTTP POST size depends on the server receiving the data. For example a PHP server has the post_max_size property that (I think) by default is 16M. In terms of encryption start by making sure the request is being sent over HTTPS.
Do you have Gzip option turn on in your Apache HTTPD server?
This way the web server will compress and serve slightly packed files to and bro your browser.
I want to send openCV Images via UDP Socket to a Java Server (run with GWT). How can I convert those cvFormats to java-workable datatypes?
I once had to work on something similar to this, what I did was to serialize the raw binary data of the images and send it via UDP. At the other end I re-created an image with the server understandable type.
Don't forget that with GWT you could also send URLs of your images (say on the image source server). So you would have a dynamic URL only generated when you need to display it at the client side.