Send parameters along with file in android to server - android

I am using this code to send a file to server
upload an image and audio in One request in android
Now my problem is I want to send some string parameters along with request.
How can I do that?

I don't have any hands on experience with this, but from looking at your code, I would think you could do it in the code below
entity.addPart("NEW_PARAMETER", new StringBody("some_value"));
Couldn't you just add more of these, and get them in your server side code just like your accessing the other values your sending?

Related

Recover an image from an android to send to server

I am currently working on an android application and I would like to add a file system to send images. But I don't know how to get an image from the local storage of an android, nor how to send it to a server. I am currently using a flask server to retrieve JSON files, and I don't know if retrieving images would be possible with this kind of server.
Would you have any clue how to retrieve a file on a button press and send it to a server please?
You can use this library to pick from local storage and upload it to the server with ease
https://github.com/gotev/android-upload-service
At this point, it doesn't matter what server you use as long as you can retrieve data.
You can do it using Retrofit. For detailed instruction, check This medium post!

Build a restapi in android from JSON

Suppose I have this json file
[{"item_id":"1000","item_name":"PEn","item_quantity":"2.66","item_rate":"3.69","item_purchase_date":"2020-05-13T00:00:00Z"},
{"item_id":"1004","item_name":"box","item_quantity":"63","item_rate":"20.5","item_purchase_date":"2020-06-12T00:00:00Z"}]
I want to make a rest api using this json file in Android. This json file should be locally available in my android device. When I send a certain request (GET/DELETE/etc.) to a certain URL (localhost:3000/posts), I want to perform requested operation.
Please show me how can I achieve this.
UPDATE 1: I don't need NanoHTTPD. (Its better if I can find something like golang with postgres)
To serve an api on an android device, you would need a webserver. The easiest way would be to extend a Java HTTP server and add your own logic.
Try NanoHTTPD, you can easily use it in android.

Android + NodeJS: Client-Server communication

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.

How can mobile app get a JSON form to create a new record in Rails web service?

I have a model User in Rails app. It is restful with 7 actions in UsersController. When accessed using a browser,
GET http://mydomain.com/users/new
will get a form to fill information on the new user. What should an Android app get? Should it also get a html form, fill it then send back the html? Can it get a JSON response from the new action in UsersController or maybe skip this step all together? How should this work?
If Android app is a client of your backend service it should have its own layouts/forms etc. Then, after collecting all necessary information from user you can send a response to server using JSON, in example:
POST http://mydomain.com/users/new
{"name":"Anrnold","password":"I<3steroids"}
If you want to display custom form retrieved dinamically from server you can pass form's fields and generate form on android device programatically. But why would you want to do that?
Everyting you need to know about json is here: json.org. I also recomend Gson

How do I read in a single line from an external text file (on server) to display in Android activity?

we have a text file stored on our server where I work, and I have searched on how to read in a specific line from that text file and have it display on the screen of my Android activity... but I haven't found a solution yet. Only two words, or so, of the text in the file on the server will be changed/replaced periodically, and I would like those changes to take effect/be updated in my Android app each time. Not sure why I couldn't find an answer to this yet. Any help is much appreciated! Thank you!
Actually the best way to have a secure transaction between an android app and server is to use web service like JSON. You request something from your android app to server, Then in server request will be analyse and response to your android app. You can send some request that tell the server what to do. Then in server side have a script that do your job and send response to your android app.

Categories

Resources