What is the best way to deal with web services in Android?
I have been looking at KSoap2 library and RESTful web services.
I got one working with KSoap2, but only when the web service is on a remote server, not on localhost. I have tried to redirect the ports for incoming and outgoing as it says on the android dev site, i have used the ip of the network i am on, my own ip and the localhost-ip (it says on the site that one cannot use localhost cause that is the emulators own loop-back network. None works.
With RESTful webservices, i have only tried a tutorial where a google app engine is used as server if i understood it right. Is it easy to connect a RESTful web service to a database. I need to have a database connection in my applikation, which one is normally used with REST?
Sorry for the confused question, it's been many days of trying to sort just the alternatives out, and the more I look the confused I get.
THanks in advance for any help!
/AK
I would go for REST for sure. You could also use JSon instead of XML. Android has a built in support for JSon and REST becames quite easy to use and maintain with JAX-RS specification.
It is also as mentioned by Maks a light weigth approach.
If you have a more complex scenario you could use some JSon librarie like GSon for JSon parser between java objects and JSon.
Also if you decide to go for Seam in the server side I would suggest you to use RestEasy.
[]s
To connect to a webserver running on the same machine as your android emulator you need to use the IP address 10.0.2.2
see here:
Once you have your app in the emulator connecting successfully to a webserver running on the same machine you can then decide how best to send data between the two of them. If you choose to use XML and/or SOAP then kSoap2 is your best bet. Using a RESTful webservice is much more "light weight" and just refers to using normal HTTP and urls for your app to get/send data to the app running on your webserver.
check this Manual, when connecting Android to RESTful Web Services: Connecting RESTful Web Services on Android
Related
I am new to mobile applications. I am basically from a web development platform. I am just playing around mobile frameworks like App Framework, LungoJS, Jquery Mobile, kendo etc to gain some knowledge in this vertical.
The app I am developing is still in UI level. All I need is to fetch data from the server and populate in my app.
I need some ideas to establish server communication between the smart device and the server. My questions are
What kind of server needed for mobile applications ? A cloud or a
regular web server is enough ?
What are the ways to connect the app with the server ? ( on cross
platform mobile development )
What is the secure way to communication with the server ?
What kind of server needed for mobile applications ? A cloud or a regular web server is enough ?
Because you are creating a hybrid mobile application you can use any type of server side technology, it doesn't matter is it a classic web server technology (using Java, PHP or .NET) or some kind of cloud technology like Parse.com.
You also don't need to create anything from scratch. Best course of action would be to use some kind of micro RESTFul framework(like PHP Falcon or Java Play Framework). Read more about them here.
But, there's always a but. You can't use server side technology for classic content generation, you only need to use it to send data to your hybrid application. I will explain this later.
There's also an alternative to RESTFul services, you can create a webservice, again using Java, PHP or .NET.
What are the ways to connect the app with the server ? ( on cross platform mobile development )
You would use AJAX as a technology (in case of RESTFul), rest depends on you. You would probably do it in JSON format (or JSONP if you are doing cross-domain calls, but you don't need to think about JSONP when creating a hybrid application).
If you intend to use a web service then you would use a SOAP connection and communicate via XML format.
No matter which server side technology you use you will always use AJAX on a client side.
Now let me tell you why you should not generate your content on server side. Basically nothing can prevent you from doing that, you can generate your complete page on web server and just show it in PhoneGap app, it would still be a hybrid app. But, if you try to put this app in Apple store you will get yourself rejected.
What is the secure way to communication with the server ?
Security of course depends on server side technology. Every framework has its own kind of security handling, but all of them relay on HTTPS so you should not worry too much.
From the client side you can always encrypt JSON/XML data and send them using POST.
Examples:
If you want to use jQuery Mobile then take a look at this tutorial. It will show you basics of client - server side communication.
Since you are new to mobile application, ill try to give short answers
1) What kind of server needed for mobile applications ? A cloud or a regular web server is enough ?
A regular web server is good.
2) What are the ways to connect the app with the server ?
via web-services
3) What is the secure way to communication with the server ?
Use HTTPS webservices (SOAP, REST), HTTPS secures the transmission.
Above is a basic explanation for your quick help, I would recommend you to go through the documentation, and review some sample codes
This will really help you Sample
Please go through this link it will surely help you
http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
Webserver,cloud anything is good for restful service
for security purpose you can use POST parameter to send and recieve data or if you want more security then you can encrypt and decrypt data through secure algorithm
I was reading that SOAP is really not for Android. Rather Android prefers REST web services. So my question is, are there any other kinds of restrictions ? For example, some web services are built with JAXB. Is that ok for Android apps ?
I other words, does it matter how the REST web service is built ?
What if the web service returns data in XML format, is that ok with Android ?
It does not matter what kind of Webservice you are communicating with.
You communicate using HTTP protocol. If you use SOAP then you can parse your xml at client side.
Also there are many SOAP clients already available so you dont have to do much work.
Check this - http://code.google.com/p/ksoap2-android/
There is no restriction with REST web services on Android. Yes android can handle xml. You can get detail knowledge and help by going through GAE (Google app Engine). Find the link below:
https://developers.google.com/appengine/docs/
Android does not provide a built in SOAP client, there are many third party libraries that can be used to call a SOAP web service with native android APIs.
Where as REST is so simple & can be used directly without any third party libraries.
I'm creating my first android app. My app works in the following way:
User log into the app.
The login details is on an online server which is SQL Server 2008 R2
The server will return data to the app and the data the data will be stored on the app's database.
The problem I am facing is how should i connect to Sql server database. I researched on this topic and found out that I need to use some service. Can someone suggest as to what service should i use? A normal .NET web service, WCF service or Java Service.
I also need to know how to consume this service in my app.
Softwares I have with me:
Visual Studio 2008
SQL Server 2008 R2.
Android Development Kit & Eclipse
SQLite
Please help me.
I highly recommend the Asp.Net Web API for this:
"ASP.NET includes ASP.NET Web API for creating rich REST-ful Web Services that return JSON, XML, or any kind of content the web supports! ASP.NET Web APIs can provide data services to mobile apps like Windows Phones, iPhones, Android and more. ASP.NET Web APIs can be used in any ASP.NET Web Application, including ASP.NET MVC, Web Forms, or Web Pages – it's all One ASP.NET."
http://www.asp.net/web-api
Just to get you the general overview here;
You do not connect your (Android) app directly to the remote SQL database/server. Android, nor any other mobile platform, provides a steady and reliable interface for doing so.
Secondly, what you want to make indeed is a (REST) web-service.
As long as your needs are simple (passing along a bit of simple data between your app and your database) this means nothing more then having some web language take up some GET or POST variables and putting them in the database for you. And the same the other way around.
You can do this in any (web)language. The choice for language might be most influenced by the server you want to run in on, and what languages it already supports.
I intend to write a multi platform smartphone app (currently only I-phone and android).
Which has to send and recieve information from a web server I intend to create.
The web server will do all the algorithms, and handles also DB connection.
My question, is how is this best accomplished, which kind of web-server technology fit best the scenario, and supports connections from various devices.
Basically, I thought about implementing a simple TCP/IP protocol, making the app (on the phone) the client, and server on the web on the other side. however, I want to deploy the application to an application server (maybe google app, JBOSS, etc.) and I don't want to be stopped by various firewalls.
does anyone has an idea ?
edit: few things are certain, the application server will be written in java, and db will be mysql.
This is a very broad question and any suggestion about which backend technology to use will depend on your language preferences, your other requirements, etc.
For starters, I'd suggest JSON over HTTP as a transport mechanism: it's easy to parse on both client and server-side, and it's directly usable in Javascript should the need arise. XML is another choice, but it can be annoying to parse.
JSON-over-HTTP (or XML) will be completely device agnostic and won't have the firewall/proxy problems you'll run into trying to do a custom-implemented TCP-based protocol.
For the backend, may folks use MySQL or Postgres for their database, and connect to it from Java, C#, Ruby, PHP, or other server-side languages. Use what you're comfortable with or what you want to learn next.
Why not write the server-side as a regular web application - in whatever technology you like (php, asp.net, java)? This way you can deploy the app on any web server and your client apps on the phones would simply establish a connection to an HTTP server. Normally, firewalls would not be a problem in such situation.
I have used this setup for my apps (both android and iphone) - connecting to a web server app written in php with postgres back-end.
I would like to connect to a MySQL Database hosted on the same server with a Tomcat Server from my Android App without using PHP. Is there any way to do this? I found some solutions but all use PHP.
Can I connect directly or do I have to do it through a Web server?
Because you're running on Tomcat I imagine the best way to access the database would be to write a webservice that handles the communication between the app and the server.
The webservice will be written in Java to run on TomCat using JSP.
I'm guessing you're trying to communicate directly with the MySQL database (i.e. run SQL commands on the database directly) but I don't think you can do this (although never tried or looked into it), I have always been under the impression that you need some code on the server to sit inbetween.
It should be common sense, that directly communicating with databases over the web is a "no go" security wise and with mobile devices a pain regarding the connectivity.
Setup a webservice with JSP or Grails (which I find comes with less workload) and deploy it to your tomcat server.
Hence the thought, you already have a Tomcat running, one assumes you have a java web app running. Try adding a webservice to that app or look in the documentation, if there already is one.
You actually cannot do this with Android the main reason is performance it is really expensive to keep a remote connection alive than rather just call Web Services on demand, and it is more portable.
So i recommend you to play around with your favorite language creating services that access to your database and digest the output (XML or JSON) with android.
BTW i also think this have been asked Android MySQL Connectoin and here is a nice tuto about it (but with PHP) it should be fairly easy to do it in Java.