I plan on consuming the REST services provided by ServiceStack outside of .NET. I plan to writing clients for java and obj-c. This should be easy since it is a REST service, however in the documentation it seems to suggest otherwise:
But ServiceStack includes two clients which are optimized for ServiceStack in aspects like exception handling etc.
How would I go about implementing a ServiceStack client with Android? Just follow normal REST consumption procedures? Should I worry about any exception handling issues?
Also, would authentication be an issue (I'll be using BasicAuthentication)?
ServiceStack web services provide pure HTTP/REST APIs which are essentially just serialized POCO DTOs over the wire. You control the entire HTTP output as ServiceStack doesn't add any additional cruft around your serialized payloads.
Expects Pure Serialized DTOs
The opinionated nature comes from the ServiceClients assuming that the services are just returning pure serialized DTOs (i.e. without additional cruft) which it simply deserializes as-is into the specified response type. The ServiceClients are unique in that they give you a strong-typed end-to-end API without any code-gen since you're able to re-use the POCO DTOs you defined your web services with.
Fallback to pre-defined routes
The APIs that don't speicfy a relativeOrAbsoluteUri in the API like the Send<TResponse>(dto) will by default use the pre-defined routes automatically provided by ServiceStack allowing you to call web services without needing to specify any Custom routes for them. You can of-course opt to use your custom routes instead by sticking to the IRestClient and IRestClientAsync APIs which allow you to specify the url to use.
Automatic Error Handling
The C# ServiceStack ServiceClients are optimized in that by convention ServiceStack web services will serialize structured error responses in a ResponseStatus property on your Response DTO (only if it has one). All the service clients do is just de-serialize the error response into a typed C# WebServiceException which gives your C# clients strong-typed access to structured errors as explained in detail in the Validation wikipage.
Basically the ResponseStatus property is just a convention (not some anti-REST magic) and you still control what HttpStatus Code and Description gets returned. It's simply another pure DTO property that all REST clients have equal access to - here's a simple JavaScript ss-validation.js routine that extracts the responseStatus metadata and injects the errors onto a Twitter Bootstrap HTML Form.
Related
I was wondering if there are some commonly used techniques for synchronizing an app with a server? I have looked at the socket-framework and maybe some http? I am thinking for instance when making a game or a grocery list, any developers out there who want to share?
I would say that the most common method is to send client data via HTTP POST using SSL/TLS encryption. The data can be sent in any format, but generally it is structured as a JSON or XML message.
The hardest part is that you need some form of authentication in order to identify the user and update/sync the correct information on the server database in a secure manner. Have a look at this article for some basic concepts.
In android, you can use the excellent Google Gson library to convert Java object to/from JSON objects.
Http communication can be performed via the Android API methods.
Can any one tell what exactly meant by web service in anddroid. Every where am able to find how to call webvservice using different protocols. but am not understanding what exactly a web service.
Web Services are similar to web pages in the sense that you access them over the web using HTTP. The difference is that you are typically getting raw data back instead of a presentation page. For example you might get a row of records back like addresses. So the transport layer is HTTP. The data for Android is typically going to be encoded in JSON, or XML. And you can access the service using HttpClient or URLConnection. Often you can test a web service in a browser to see what you are getting back and you will usually see JSON or XML formatted data. For JSON you use a library like simple_json to extract into java object. XML is usually processed using a SAX Parser. They are really quite easy to use.
A Web Service, in Android, is just like any other computer, a request for information over HTTP.
"Web Services can convert your application into a Web-application" (from w3schools.com) means that your can use Web Services to provide a richer and more up-to-date experience. For example if your application is a text editor it could check a web services for updates rather than force the user to check a website for updates. Another example is writing your own client to Facebook which would use several Web Service calls and depend on Facebook for content to present to the user.
A web service is a standard used for exchanging information between
applications or systems of heterogeneous type. Software applications
written in various programming languages and running on various
platforms can use web services to exchange information over Internet
using http protocol. This inter-operability can be achieved between
Java and Dot net applications, or PHP and Java applications.
For example, an android application can interact with java or .net application using web services.
What are the best ways to connect site and show it's data on an android application ? Also does I have to create anything on server where the site is for using JSON ? I am new to programming web android application's, though I searched a lot I didn't find anything which would explain me straight to the point.
You're on the solid ground starting out using JSON as the interchange between the two.
Alot of popular mobile apps like Twitter and Foursquare have restful APIs set up to interact with their mobile clients by exchanging HTTP requests that contain data formatted as JSON. Most of the communication between the two can be accomplished with HTTP requests using the standard GET and POST methods.
A good place to start would be setting up some server endpoints that output this data and then setting up your android app to request and parse this data just like a browser would. You just need to set the appropriate mimetypes on your server end (application/json).
Most modern server-side languages have implemented modules/functions that can take their native data structures and approximate them in serialized JSON (PHP's json_encode(), python's json.dumps() etc) These can be used to output data from within the app or database to your mobile client where it can be interpreted and used in the Java environment there.
To pass back JSON you need to set the mime type (http://stackoverflow.com/questions/477816/the-right-json-content-type), which is application/json.
If you are passing back JSON or XML then the client just needs to make the appropriate http call, most likely GET, perhaps POST, to actually retrieve the information.
You can use something like this as a starting point:
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
I'm looking for some best practice concepts as far as transferring data between a mobile device (Android right now, but concepts apply pretty much to the rest as well). I currently have a WCF service set up with a working JSON endpoint. I'm starting to modify the existing service methods with the appropriate WebGet/Invokes, etc to make it RESTful. The service implements the request/response pattern so that all communication between a client and the service are wrapped in a complex MessageRequest and MessageResponse object.
What is the best way to have a mobile application successfully utilize this pattern? There are only two solutions I can come up with, each with their own pros and cons:
Create all the data transfer objects in the client project, and then create a JSON/DTO mapper (GSON might work well here). Use the client-side objects to handle all client data management until a server request is necessary, go DTO-to-JSON, and send the request to the server. The upside here strikes me is that it makes client-side data management easier because it parallels the service domain. The downside is that these have this has the potential to breakdown the more complex an object becomes.
Ignore the DTOs client side and just do everything straight from the JSON. The upside here is that it removes the overhead associated with the larger objects and the required mapping. The downside here is that this strikes me as being very brittle - any changes to the returning object need to be handled deep in the code, rather than just making the change to the client side DTO and mapper.
Is there a better way to accomplish this data exchange? Or are these the only real ways to handle it? How do you manage data transfer in your mobile applications?
I have a very similar WCF setup as you do, and I ended up creating very lightweight data objects client side. These manage pulling apart a JSONObject representing themselves and create any sub-objects they need, but aside from that are simple classes mostly used to group data together and contain no business logic. We haven't yet needed to do any client side caching, but these objects would be a great place to put in SQLite code to persist themselves out.
It has worked great so far, and we were even able to port the client-side Android code to another project running regular Java just by including org.json.
I am writing an Android app that talks to a Google App Engine server. The server holds persistent data, which it stores and fetches using PersistenceManager. The way I have this set up now is as follows:
A #PersistenceCapable class on the server called StoredThingToRemember has the information to remember, as well as some GAE object persistence jazz.
When the Android client wants to fetch a ThingToRemember, it sends an HTTP request to the server, which fetches a StoredThingToRemember from a PersistenceManager, converts it to a ThingToRemember implements Serializable, serializes it as a byte[], then sends it in an HTTP response.
The client unserializes the ThingToRemember and uses it.
This works, but it seems wonky. Ideally, I would like to serialize and send the StoredThingToRemember itself. Unfortunately, that seems to require putting all the GAE object persistence classes in the Android app, which seems silly and wasteful.
What is the correct way to grab an object from GAE persistence and then use that object in an Android app?
Using serialization formats for transmitting data is generally fairly risky - they're usually not designed with transmission across trust domains in mind. Further, by doing so you're locking yourself in - both your client and your server will always have to be written in Java. Any further clients will either have to be written in Java, or will require a whole new interface.
Instead, you should serialize to a language-independent format, such as XML or JSON.