My client is an Android app and my service is an asp.net web api. I’m the only one using my service. I am trying to duplicate, in the Android-REST world what I am already doing in the Microsoft Windows Phone 7/ WCF SOAP world. I have numerous methods that both receive complex objects and return complex objects.
The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
But when I post questions about doing the same thing in REST, I’m told I should limit my services into GET, PUT, POST, and DELETE only. And that I should only do what is “proper” according to RFC2616. Some speak of this in almost a religious manner.
Forgeting about the religion, what’s wrong with using a GET for everything? Or what’s wrong with using a POST for everything? What I do does not fall into the simplistic RFC2616 categories. For instance I’m passing a thousand legs of a trip taken in a car and I’m getting back another version of that trip with erratic GPS errors smoothed out. Or, I’m sending a conversation in english and getting that conversation back in German.
In the android client I have the objects I want to send over HTTP already serialized into json strings by using Google-GSON. So my questions are…
How can I send these json strings to my REST Service as arguments in either GET or POST?
Is it possible and feasible to use just all GETs (or all POSTs) for all of my calls to my REST Service and how do I do that?
I have a more pragmatic question about this posted at sending a json string in a http url as I can’t find any examples anywhere of sending json strings over http GET or POST.
Thanks, Gary
Using the good HTTP verb is very usefull to simply know what to do when you request failed (for example) or just to do some specific stuff. If you sent a POST request, it's implicitly suppose that you have to parse your resource in order to obtain a stream which be sent via the request's body. In other hand, when you are retrieving data via GET, it's suppose that the request is gonna be sent back to you as a stream that you will mapped to your model, pojo, or anything else.
I can suggest you to use library such as RESTDroid. You can send POJO and receive POJO. It's a "resource oriented" library, so you can know at any moment if a particular local resource is remotely syncronized. Data persistence between local and remote is automatically handles.
RESTDroid is alpha released. You can have a look to RoboSpice. It's a powerful library to manage REST call but it's up to you to manage the persistency between local and remote resources.
1) The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
- IN REST:"You can pass any complex arguments you want and return any complex results you want too.
2a) Forgeting about the religion, what’s wrong with using a GET for everything?
In SOAP services WCF/or classical you are wrapping all requests into http POST so using single verb would end up to SOAP or - don't even think about it - your own communication protocol:-D
2b) You can technically compose GET request with non empty body - most of the servers ignore it by default though and it would be technically problematic to read it..
the other part of the question is answered by Pcriulan above
Related
first post.
I am building an android app and the backend (remote server) im using ktor with exposed and hitting a postgresql data base. I am just unsure upon getting my results back from queries how I should attempt to send those back to my user app on android. What would be the right way. To have the user app install exposed library and just send the query results ? format all the results to a json string and blast that? Just dont know what would be ideal.
I haven't really attempted anything yet because I was looking for a "best practices" idea.
Hi and welcome to Stackoverflow.
There is no one "best practice" way to send the data from the server to the client. Noramlly it depends on your use case. However, one common way that I would suggest you use is to create RESTful-API.
This means:
Requests and Responses to/from the server will be serialized to JSON.
Confirm to HTTP verbs semantics (use GET to read data, PUT to update, etc).
Use status code for error handling
Follow the REST API guidelines for best practices (API versioning, end point names, pagination, etc)
To read more about REST API guidelines, this page is useful.
For Ktor, this will mean having a serializer, and then defining your routes. This guide is useful for Ktor.
Although this is not the only way, but it will provide you with enough experience to explore others APIs (Custom API, RPC, SOAP, etc).
I am interested in making android app like crickbuzz that will fetch live content from server and also there is option of like and comment for users for feedback. So, can anyone tell that how to implement this i.e (how to fetch data from server) how that data will fit in app(confused about this how data get fit in instagram app means profile, home, search tab)
thanks
I suggest you check out this links
Android, PHP & MySQL
Fetch Data From Server
Ok, let's break it down:
The data is obtained from the server using http requests. It is usually received in the form of JSON String, basically a ordered, human-readable data format which is easy to read and parse.
On the app side, you need to implement the layout, which is done in xml and Java. You also need some logic which will obtain the data with the aforementioned requests, parse it, turn into Java Objects and put everything into the layout.
The process is not really complex, could be completed by a single person, but you will need experience, time and good architectural decisions.
If this is your first time with Android app programming, do some tutorials (especially ones that cover networking), learn some libraries (Volley, Retrofit, org.json, RXJava, Picasso will be really useful for what you ask about) and code several small apps from start to end. After that you may be ready to tackle the app you described :-)
I am trying to make an android app that requires a user to login against a MS SQL Database.
Having read around the most popular way seems to be to use JSON to do this however I'm not sure how secure this would be (especially if there is no SSL being used).
My question is what are the alternatives available and if JSON is the best/easiest way to achieve this how can I make it more secure? Is this also how big companies (such as dropbox etc) do this?
When I first started Android programming I was told that making a direct connection to the database is considered bad practice, and that an interface (JSP, PHP, .NET) should always be used. I don't know if this is a security thing or not but it would probably be the best for you.
If possible, create a .NET (or whatever server-side language you are comfortable with; .NET would probably be the easiest if you're working with a MS SQL server) page and talk to it over HTTPS (there's your security) and pass it the login info using POST. You could use JSON but name value parameters would accomplish the same thing. Have your page connect to the DB and test the information you pass it against whatever is in it. Then pass back a value that says whether it is correct or not in the response.
EDIT:
This looks like a decent guide to getting HTTPS set up on Android (just browsed through it quickly so I can't vouch for it 100%).
JSON is just the form you pass data in.
Noone stops you from using SSL for the connection. Or encrypting the JSON data in your own way.
I am just learning about Android Development so excuse me if this is a bit off in nature.
I am wanting to make an app that interacts with a Database from my website, in a sense the two things will be one feeding the other. So with that. I am trying to figure out whats the best way to interact with my server. I don't want an app thats an app in a browser like environment I want to dev a full app that works independently of the site only sharing the DB and like features. So what would be my best approach?
Is building the app so it can post/get to php files on the server interacting basically through JSON/XML my best and or safest bet or is there a better approach that connects the App to the servers Database that doesn't require me to open the database to any ip that makes a request.
Just looking for opinions and suggestions here. I figure everyone who's going to see this is familiar with Android development and best practices where as I could and have surfed blogs and all else but the opinion seems to be 50/50 as to which is best.
I'm sure there are libraries out there for Android that help you with HTTP Get and Post, however, if you really want to understand what is going there are just a couple of classes you have to understand in order to make the necessary classes yourself.
First, get to know HttpClient, HTTPGet, HTTPPost, and HTTPResponse. Some of the later versions of Android have some nice other classes as well, but those four is pretty much all you need to get started.
You need to do something like this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.myurl.com/api_name");
HttpResponse response = client.execute(request);
If you debug this (with a real URL of course), you'll notice that your app kind of freezes during client.execute(). This is the point at which the request has actually fired and the app is waiting for a response. Once you actually get the response, it isn't very difficult to get the data out of it.
Once you understand this, you'll want to get to know AsyncTask, which is endlessly useful for performing background tasks. You can find the documentation here: http://developer.android.com/reference/android/os/AsyncTask.html There is a great example of how to use this right at the top.
Using these two concepts together you can perform asynchronous HTTP requests. Basically, put your actual HTTP execute code in doInBackground of your AsyncTask. At the end of the doInBackground return your response, and then do what you want with your data in the AsyncTask's onPostExecute.
We've found that providing a proper RESTful web API that hits the database on the backend in whatever language you choose (be it PHP, RoR, whatever) provides a useful interface for any number of uses (your own website, mobile apps, etc).
Then it's a matter of your Android app interacting with the RESTful API, which is simply HTTP requests. Those can be encapsulated in helper classes to make them straightforward as well.
Based on my experience, the best framework for doing RESTFul things with Android is: Spring Android
From a client perspective, it provides all the tools needed to access secure RESTFul services. Since it is Spring, it provides nice abstractions over most of the boiler plate http code. As an example, it provides a clean way to perform a GET that returns json, and then serialize that to a POJO.
As an example:
RestTemplate restTemplate = new RestTemplate();
// Add Jackson JSON Message Converter to Template
restTemplate.setMessageConverters(
new ArrayList<HttpMessageConverter<?>>() {
{
add(new MappingJacksonHttpMessageConverter());
}
}
);
// Simple Conversion - pojo is now populated
MyPojo pojo = restTemplate.getForObject(url, MyPojo.class);
The approach you mention in the question: PHP on the server and JSON for requests/responses, does work. But getting it perfect can be tricky.
I found it helpful to have small request/reponse classes for each call on the Android side, like SaveNoteToServerRequest, SaveNoteToServerResponse classes which are just plain java objects with whatever fields are needed for the request/response. Then you can use a library like GSON to convert the request object to JSON and convert the http response from JSON to the response object.
On the PHP side you can create a small class for the response object, then json_encode at the end.
That way you're not directly manipulating JSON objects, just using your own plain java objects or php classes most of the time.
Hope that helps.
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/