Send JSONObject from android app to node.js over http? - android

Im having issues finding tutorial for this subject. Do anyone have an good example.
I want to send an JSONObject from a android app over http to node.js.
Thanks

OK, start by learning about HTTP & REST to decide whether you want to PUT or POST your JSON.
Create a HTTPRequest in your Android app that puts the JSON into the HTTP message's body. See:
How to send POST request in JSON using HTTPClient?
How to send a JSON object over Request with Android?
Back to node:
Set up a node.js http handler for that type of request. I recommend using express, since it's easy and you'll find the most examples & support when using it.
Use a body parser in express to read the JSON object.
$$$ PROFIT $$$

Related

Android - Retrieve data from server database through web service (JSON v/s Servlet)

I need to retrieve huge amounts of data from a database through a web service from an Android app. I have two different ways to do this, and I wanted some advice on it:
1. The first option is to create a .php file on the server side that managed any POST coming from the client (Android app). The server would then create a JSON response. Finally we would parse this response using a JSON parser in Android. This is also known as the REST scheme.
2. The second option is to create a SERVLET, execute it from the client (Android), have the servlet send the request to the database for us, and finally parse that data from Android. Obviously the servlet would be written so that it could easily interact with the database.
Points to note (so as to decide which option is better):
1. I won't be storing anything in the database from the client. That is, my Android app is read-only.
2. I will be reading from a huge database, so it is a priority here the performance of the Client-Server interaction, with a special mention for data parsing and for servlet vs php performance.
Any help would be greatly appreciated.
Android has built-in support for parsing JSON data with the use of JSONObjects and JSONArrays, so it would be a lot easier to handle data in that form, rather than handling servlets. Its even possible to directly receive the web service response as a JSONObject or JSONArray.
In general, web services in Android should be of the RESTful type. That's how Google seems to prefer it. That's why there's built-in support for JSON, but not for SOA or Servlets.
References:
1. Reasons for not directly write Servlets for creating a REST API.
2. Servlet vs REST.

Pass parameters Using HTTP request

Hi i am new to android programming and i would like to know how to call a web service and pass parameters to the web service to obtain the return result in form of an XML.
I previously used Ksoap lib and SOAP request but thats not working correct so now i am trying the HTTP method.
Please Help
You will want to read up on the Apache HttpClient.
Beyond that its a matter of making sure the device and server are generating messages that the other side can correctly parse.
Is the server an existing service? Or are you designing that as well?
check the following in which you will use HTTP post request & you will get response in the form on XML
Android HttpPost: how to get the result

RESTful RAILS and Android HttpRequests

I am currently working on building an Android app for my Rails backend.
I created api_tokens_controller to create an api_token when user logs in from Android using HttpPost and save that token both in Rails user table and Android shared preferences. I was able to verify this.
I am trying to send the api_token as params[:api_token] from my Android app with every subsequent request after initial log in and have Rails verify:
In User.rb:
def self.authenticate_with_api_token(api_token_from_mobile)
user = find_by_api_token(api_token_from_mobile)
if user.nil?
return nil
else
return user
end
end
if user exists, then send other data as JSONObjects.
I am running into trouble conceptually here. I am now trying to have the Android app go to
...:3000/users and HttpGet user data as JSONObjects. The problem is that I don't know how to send the api_token from Android app to Rails in HttpGet. I am assuming that since it is RESTful, my HttpRequests have to correspond to the way rails routes work.
Questions:
Is it possible to send in JSONObject of params[:api_token] from android app to Rails in HttpGet?
Do my HttpRequests have to correspond to the RESTful way Rails works or can I just only use HttpPost?
Thanks
If I understand you correctly, then you just want to add the API token to as a URL parameter if using GET, so something like:
...:3000/users.json?api_token=token
This will put the api_token in the params hash just the same as POST data would be. The RESTful way would be to use GET for retrieving data from the API and using POST to send data, i.e. changing data on your backend but if you are going to be returning a user with the request then GET is the correct method to be using anyway.
I'm a former Ruby on Rails developer myself.
I agree with Tom that you can just pass in the api_token as a parameter with an HTTP GET.
For your second question, I'd say that the RESTful way is to use all the HTTP Verbs for the different actions that you can take in the API. You could change your routes to use HTTP POST and then supply extra parameters to differentiate between say Create and Update (to emulate a PUT). However, I don't think that's the way forward.
However, the good news is that whether you use HttpClient or HttpUrlConnection in your Android app, you will have access to the different HTTP verbs you need. For HttpClient, it is actually different classes like HttpGet, HttpPost, HttpPut and HttpDelete which you then configure by supplying URL and other parameters. For HttpUrlConnection, you just call it like so:
setRequestMethod("PUT");
Here's the relevant documentation on HttpUrlConnection:
HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called. Other HTTP methods (OPTIONS, HEAD, PUT, DELETE and TRACE) can be used with setRequestMethod(String).

How to perform client-server communication with android app? Can I use servlet on server side or do I need to implement web service?

I am developing an android app where a user has to enter his login details for authentication...the app will then check username and password entered by user on server and will act accordingly..so how to pass these login details to server..and do I need to implement web service on server side or can use servlet?
Can anyone please tell me which Web Service should I use for the above purpose...also I need to send other data from my client app to server, process it on server and give back the reply to client app...also how to implement a web service? Are there any tools for it? Any tutorials would be of great help.
Thanks in advance...
well, to send data to the server you can use HttpPost and HttpGet requests.
and as you need to get results from the server then, yes you'll need a web service.
You can use servlet (or) webservice, any service, as long as it can be accessed through URL. Make sure the servlet/webservice returning XML/JSON (one of these two formats are preferred, you need to code your servlet/service to return response in one of these formats) response when you hit the URL in internet explore. Once you make sure URL is returning data, then
1) In your app, using HTTPClient, invoke the URL
2) You will get either XML/JSON
3) If it JSON, use in buit JSONObject to parse the response and get the data
If response is XML, use either SAX/Dom parsing to parse the response.

Android - What is the best way to send a JSON to a remote server and wait for an asynchronous response

I want to send a JSON object to a remote server from my android mobile client. I'm a bit confused from the examples I found, Some uses Sockets the other HttpClient.
Is There a way to define a callback method?
What would be best practice in order to get an asynchronous response?
Please Check out the below link :
http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/
http://www.vogella.de/articles/AndroidJSON/article.html
http://www.helloandroid.com/tutorials/connecting-mysql-database
There you will detailed explanation for sending and receiving the data from server. By taking reference I have write Program to send to the server and receiving from the server.

Categories

Resources