I write app on android which will need to exchange xml data with http server. I wonder what would be the better approach. Send whole file via POST or maybe get all text from file put it on String and then send this String via POST. Is there will be some difference? If yes what is better option?
I would strongly recommend using POST. While sending the file content using GET is theoretically possibly, in some cases you may encounter problems when using URLs over 2000 characters in length. RFC imposes no strict limit, however some clients and servers impose their own limit. Look at this question for more details on this.
With POST this wouldn't apply and you can send (almost) any size data. To send the file, you would still need to read the content of the file and send it as POST parameter though. Again, in reality, most servers will not accept more than just below 2GB, but that's a separate issue.
Related
Am trying to develope an android application that send images to my server for processing reasons ,what is the best way to configure it inorder to recieve data from android users(am using http req)
Thanks
Apache is simply a webserver. Generally when you send images they are sent as POST data. This means you'll need to make sure that Apache can handle the sizes of the images that are sent to it. The directive LimitRequestBody controls this limit for Apache. It's set to '0' by default. This means it wont limit the POST body size.
The next thing you need is have a server side script take care of the processing, storage and response to the client. You may use ruby (Rails), PHP, Java to accomplish this. Each framework has default limits. For instance PHP has 'upload_max_filesize' directive which needs to be set if you expect the images beyond the default 2MB. This is set in the php.ini.
I have some questions about developing a Android application which shall be able to communicate with a NodeJS server.
The Android application gathers some data and saves everything in a .csv file.
This file now needs to be uploaded to a NodeJS server. The NodeJS server should save the file as well as storing the content in a MongoDB.
My question now is how I should implement the communication between the Android device and the server.
I know how to upload a single file to a NodeJS server using a HttpURLConnection with a DataOutputStream.
But I need more than just uploading the file because I need a unique identification of each Android device.
I thought about using the (encrypted) Google account E-Mail address of the user to distinguish the devices. I am not interested in knowing who uploads which data but I need to store the data for each device separately.
The problem is that I don't know how to communicate between the device and the server.
If I upload a file via HttpURLConnection and DataOutptStream it seems that I can only upload the file without any additional information like the unique key for the device.
I also thought about uploading the file via sockets. But I am not sure how to handle huge file sizes (5 MB or more).
I am not looking for code fragments. I rather need some hints to the right direction. Hopefully my problem was stated clearly and someone can help me with this.
Using a HttpUrlConnection on the Android side, and a RESTful server on the Node side would be a straightforward option.
You can embed information into the URL in a RESTful way:
pathParam: www.address.com/api/save/{clientId}/data
queryParam: www.address.com/api/save/data?c={clientID}
each uniquely identifying the client. This can be whatever scheme you choose. You will have to build the HttpUrlConnection each time as the URI is unique, and important!
The server side can then route the URL however you see fit. Node has a number of packages to aid in that (Express, Restify, etc.). Basically you'll grab the body of the request to store into your DB, but the other parameters are available too so it's all a unique and separated transaction.
Edit: The package you use for RESTful handling can stream large files for you as well. Processing of the request can really begin once the data is fully uploaded to the server.
Using a socket would be nearly just as easy. The most difficult part will be 'making your own protocol' which in reality could be very simple.
Upload 1 file at at time by sending data to the socket like this:
54::{filename:'myfilename.txt',length:13023,hash:'ss23vd'}xxxxxxxxxxx...
54= length of the JSON txt
:: = the delimiter between the length and the JSON
{JSON} = additional data you need
xxx... = 13023 bytes of data
Then once all the data is sent you can disconnect... OR if you need to send another file, you know where the next set of data should be.
And since node.js is javascript you already have wonderful JSON support to parse the JSON for you.
Would I suggest using a socket? Probably not. Because if you ever have to upload additional files at the same time, HTTP and node.js HTTP modules might do a better job. But if you can guarantee nothing will ever change, then sure, why not... But that's a bad attitude to have towards development.
the Apache guide on Http connections mention that
The overhead of connection handshaking can be significant, especially
for small HTTP messages. One can achieve a much higher data throughput
if open connections can be re-used to execute multiple requests.
What i want to do is, i want to send 20 similar post request (i.e. the same headers), but the data i send with it is pretty small. So with every request i'm sending the same header data again and again.So i'm looking for a workaround.
for eg:
establish a connection(sending and using the header info, ONCE)
send 20 post requests
close the connection.
My Question : Is it, or is it not, possible to achieve that using Android's HttpClient? If Yes, please provide/point me to a lesson/tutorial/example.
Thanks in advance.
PS: i did some research, couldn't quite find anything useful.
— Edited —
I'm sorry I misunderstood your question.
What you want is obviously possible. However, I think it's up to the server side. If you code server then you can control the format of messages between client and server. If not, then perhaps you should follow server's APIs.
I'm building an Android client for a web service that accepts POST data. We're standing on the fence which format to choose for the POST data. According to me, the easiest way is to send it in UrlEncoded format but server side developer thinks JSON is better.
What are the pros and cons of using UrlEncoded / jsonEncoded / bsonEncoded format?
I would avoid xmlencoded data, but what about the others?
The answer to your question greatly depends on what kind of data you're going to send. If your data is mostly string values, numbers and the like, probably JSON would be your best solution.
Avoid url-encoded data, use MultiPart instead -- it takes a bit more work, but it's more secure (url-encoded data it's visible in the server logs) and you may send large files (images?) easily.
If you are sending maps (set of key-value pairs) and arrays, JSON is probably the easiest to work with from a developer standpoint on both client and server. If you need to optimize instead on use bandwidth usage for large set of non-media data, protobuf works well.
I'm writing an app that will periodically send information out to a remote server, and then get relevant information about other users from that server back to the local database. What's the best way to handle sending out this info (i.e.: XML or binary) and writing it to the remote server.
Also, how can I assure that, when 500+ users' data get's to the server or FTP (or better alternative?) at once, the appropriate fields gets overwritten or added, without skipping any or overwriting the entire thing? Thanks for the help.
i was looking for that answer too and i found some nice tutorials. Unfortenly they are not in english, but you can understand a princip.
This (http://vimeo.com/29332913) is the forth video of tutorials. Just click on authors name and start 3 tutorials before.
Hope it will help you XD
Janez
Have you considered JSON? FTP is generally a solution when it comes to binary data (large files, etc.), however I assume you want to exchange data that contains information in a textual format, so for a website HTTP will be a good way (the best way depends entirely on your server setup, your data format and model).
I have to add, that YAML is also a very nice data format tool if you are using Ruby frameworks, which I prefer over JSON (personal experience).