Pass parameters Using HTTP request - android

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

Related

How to diffentiate the webservice request is from android app?

I am new to android and working on an application,I am making the webservice request from android app and website,I want to track that webservice request whether it is coming from mobile app or website?So is there any way to differentiation it from the webservice request?Without using "user-Agent" in the header or without adding any value explicitly?
want to track that webservice request whether it is coming from mobile app or website?
You need to add one parameter in request header and define your device type there and same thing should be read at the server end.
First, construct an API that will service the request between the user and the client. Then test using jsonlint or Postman to determine the JSON key-value pairs..

Using HttpClient to invoke a webservice in an android app

I am new to android & web services.I I want to create an android app which consume a webservice using HttpClient.
Yet i am confused with, how to do this. I'll explain what i've needed to do.
Say,we have a webservice at http://www.w3schools.com/webservices/tempconvert.asmx
WSDL url=http://www.w3schools.com/webservices/tempconvert.asmx?WSDL
What I need is to call the method "FahrenheitToCelsius" & return values from it in an android app.Can't i do this using HttpClient? Since it seems to get only the HTTP methods like GET and POST.
When I search on this ,I found the below site as a more frequent suggestion. But it doesn't contain ,what I need to do.
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
(I could call "FahrenheitToCelsius" method & return values using ksoap library.But now i need to do it using HttpClient too)
To clarify- When you connect to a webservice via its WSDL file, you're consuming a very specific kind of webservice— specifically, a SOAP webservice.
The typically way to connect to a SOAP webservice is to either find a library in your language of choice, or find a utility that will use the WSDL to generate code for you that communicates with the webservice.
One popular utility for connecting to SOAP on Android is called ksoap-android.

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

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 $$$

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.

Categories

Resources