Android Client connecting to RestFul Webservice - java

hello All
I have been fed up of looking for a simple example of rest webservice connecting to Android Application. I know so many people have asked this question before, but there are so many different answers as a theory.
I know I must use restful web-service, rather than SOAP for mobile applications.
I need to create a webservice which is producing JSON, then in Android I can parse the JSON, but the problem is that as I am new to android I am looking for a simple example with both Server and client.
A server : JAVA with Database connection and Restful WebService that produce JSON
Android Client : which parse the JSON result from the webservice
please if somebody can provide such a example , please please help me.

Firstly, before you create your own web service, you might like to experiment with existing web services, such as those provided by Yahoo.
Have a look at the PlaceFinder API by Yahoo, its a free web service you can call, and is quite well documented. You can find the JSON examples here
Create a simple, standard java app that queries that web service, deals with JSON then System.out.print something from the response, that will get you familiar with what you need before you bring the android platform into the question
I haven't used the JSON examples, but I've written a quick tutorial on the XML side which may be of help. Once you're comfortable with this, introduce that code into an Android application and get it to print something on the screen.
You have quite a broad set of requirements in your question, please try what I've recommended then come back with refined further questions
Good luck!

Related

Android : Step for Android connect to Laravel API

I am new in programming. And I would like to ask what is the step to connect Laravel API. Let say I want to build an Android App and I want to connect to the backend that is Laravel but the point is, the backend is not created by me it is created by other developer and I'm totally don't understand in PHP.
So in this situation, what is the step or how to do Android connect with the Laravel?
PS: I have already saw some sample from other article, but the tutorial mostly are build the both Laravel and Android at the same time. So that is quite not same with my situation...
So Hope I can find out the answer from here. Thanks in advance
You can use Retrofit for creating calling API service in your android application.
This is a very good tutorial: https://www.journaldev.com/13639/retrofit-android-example-tutorial.
For Laravel API, you can follow tutorial: https://www.toptal.com/laravel/restful-laravel-api-tutorial.
The very first step to interacting with a remote REST API is to have an understanding of what the expected request and response data is. This can typically be found in the documentation of the REST API you're trying to access. In this case, since it has been created by another developer, you should ask them for the docs.
After you've gotten a decent understanding of the API, you can use one of several HTTP client libraries for Android such as Retrofit to send requests and receive responses from the API.
Note: It is not necessary to understand the API's code, only the REST endpoints and the request/response schema. A good example of API documentation can be found here

Build an Web UI with AngularJS for a java project?

I would like to add a Web User Interface for an existing Java project I did time ago. I learned basics of AngularJS on codeschool.com courses and now I know how to send an http or REST request to get data for my web UI.
My Java project has a set of methods that elaborate some data from a local database and return integers or integers arrays.
The goal of the interface would be to show a bunch of charts and data directly from that Java project.
What would be the most appropriate way to do this? I heard of implementing REST services on my Java project but it seems overkill for the purpose and i'm totally confused by all the frameworks for this. What would you use?
Thanks everyone for your answers!
I would use SprinvMVC to provide data from server to client side.
Here is my project from which you can start and learn basics :
https://github.com/xwid/thaimaster
Basicly you should create spring controllers mapped to urls, by doing it this way, you will be able to retrive server data using angular js.
http://www.javabeat.net/spring-mvc-angularjs-integration/
If you don't want to use a full REST framework such as Jersey, another possibility would be to use an embeddable HTTP server (there are several) and handle the requests yourself. This would mean that requests to something like /myapp/ would return your AngularJS filled HTML page, and requests to /resources/* would provide with REST functionality.
This would give you a standalone Java program that doesn't need a servlet container, but it would be a somewhat hacky solution. Not production grade, but you'd probably learn something from having to handle the HTTP traffic yourself.
With the Spring Framework's SprigBoot, it's quite easy to implement a REST service and have a runnable java application. You can follow this Building a RESTful Web Service guide from spring.io. They are clear and quickly understandable.
Also if you are not already familiar, through these guides you can have a glimpse of gradle and maven as well.

JavaFX2 client-server via REST Json

Which is the way to create a JavaFX client-server app using Json packets for the communication? I should interact in real time between two different remote JavaFX instances (like in a chat) sending from one side to the other messages Json formatted.
Any example for doing that?
Thanks!
If you need a high level overview of how this could be done, then you need a library for http (You want to send JSON over http, right?) and some library to work with JSON. I think JavaFX2 is not the case in this problem, you can do this in any Java app.
There is a nice thing called Apache HttpComponents to work with http. Tutorials on their site should provide you with some tips of how to implement both server and client functionality.
To work with JSON you can use, for example, Google's GSON library. Some discussion of JSON libraries can be found here.
At first look you need to form a Java object, representing JSON, convert in to String and pack it up into an http POST request.
Hope this will be some help.

Android JSON vs REST

im starting with web services. I'm working in a project that needs to communicate Android with a web server and I'd like to save some time chosing the appropriate protocol for the communication.
Between JSON ,REST and SOAP:
Which ones can I run in a non-dedicated server?
Whats the best choice for a high trafic server?
Thanks in advance
JSON and REST are not mututally exclusive. JSON is a data format that the REST interface can return.
You may run either on a non dedicated server. I would personally choose REST for rapid prototyping on Android as it is easier to get up and running. (With SOAP you will probably want to have a schema which takes time to put together)
There's a good comparison between REST and SOAP on the REST wiki article: http://en.wikipedia.org/wiki/Representational_State_Transfer#Concept
REST is more of a framework than a communications protocol. JSON and SOAP could be use to create a RESTful application.
If you're writing something with many resources, I'd use REST as it is far more structured. There are also a lot of libraries that will set most of things up for you. I find JSON and SOAP are better for custom functionality that you want to hack together quickly. They can be lightweight, but less structured.

Consume ado.net data service from android 2.1

I have a ado.net data service created using VS 2008 sp 1 that is hosted that I want to consume via HTTP and JSON from Android 2.1? Does anyone have sample code on how to do CRUD operations easily do this? I heard Restlet makes this very easy but can't seem to find sample Android code on doing this. If someone can post a tutorial with some actual code that would be greatly appreciated.
here is a good link..
http://composedcrap.blogspot.com/2009/08/connecting-to-net-web-service-from.html
It uses ksoap2 API
http://www.tuxpan.com/android-soap/android-ksoap2-build.zip
another link from right here on SO...
How to call a .NET Webservice from Android using KSOAP2?
And here is a link for Android to WCF....
Can't connect to WCF service on Android
For a RESTful WCF service, here is a good tutorial...
http://mypetprojects.blogspot.com/2009/05/communication-between-wcf-service-and.html
I found another link: http://wiki.restlet.org/docs_2.0/13-restlet/28-restlet/287-restlet/288-restlet.html
Seems usefull.
(+1 for Eclipsed4utoo for all the links!)
Reached here late(or very late) but may help someone in present or future. If you are looking to consume a ado.net data service(now renamed WCF Data Service) from android client, I would recommend you to have a look at the OData4j library. It provides an easy way to access such a service in both xml and json format. Also, it is faster than RESTlet.
Sample code:
ODataConsumer c = ODataJerseyConsumer.create("http://url/WebService.svc");
List<OEntity> listEntities = c.getEntities("Movies").execute().toList();
if (listEntities.size() > 0 ) {
for(OEntity entity : listEntities) {
System.out.println(entity.getProperty("MovieID").getValue().toString());
}
}
You can find more on WCF Data Service and OData4j here.

Categories