Android Application Development and Interaction with Web Servers

I am just studying Android Development, so excuse me if this is a bit of a nature. I want to make an application that interacts with a database from my website, in a sense, two things will feed one to the other. So it is with that. I am trying to figure out the best way to communicate with my server. I do not want the application to be in a browser application such as the environment in which I want to create a complete application that works independently of the site, only using a database and similar functionality. So what's my best approach?

Builds an application so it can send / receive php files on the server, interacting mostly via JSON / XML, my best and safest bet, or is there a better approach that connects the application to the servers. A database that doesn't require me to open the database for any ip that makes a request.

Just look for opinions and suggestions here. I believe everyone who sees this is familiar with Android development and best practices where I could and have searched blogs and everything else, but the opinion seems to be 50/50 which is better.


source to share

4 answers

We've found that providing a proper RESTful web API that hits a database in the backend in whatever language you choose (be it PHP, RoR, whatever) provides a useful interface for any use (your own website, mobile app, etc.). etc.).

Then it's a matter of your Android app interacting with a RESTful API, which is just HTTP requests. They can be encapsulated in helper classes to make them understandable.



I'm sure there are Android libraries out there that help you with HTTP Get and Post, however, if you really want to understand what's going on, there are only a few classes that you need to understand in order to make the needed classes yourself.

Check out HttpClient, HTTPGet, HTTPPost, and HTTPResponse first. Some of the more recent versions of Android have other good classes, but these four are all you need to get started.

You need to do something like this:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("");
HttpResponse response = client.execute(request);


If you debug this (with a real url, of course) you will notice that your application view hangs during client.execute (). This is the moment when the request actually fires and the application is waiting for a response. Once you actually get the answer, it's not very difficult to get data out of that.

Once you understand this, you will want to become familiar with AsyncTask, which is infinitely useful for performing background tasks. You can find the documentation here: There is a great example of how to use this right at the top.

Using the two together, you can make asynchronous HTTP requests. Basically, put the actual HTTP execution code in the doInBackground of your AsyncTask. At the end of doInBackground, return your answer, then do what you want with your data in AsyncTask onPostExecute.



Based on my experience, the best framework for working with RESTful from Android: Spring Android

From a client perspective, it provides all the tools needed to access secure RESTful services. Since this is Spring, it provides nice abstractions for most of the boilerplate http code. As an example, it provides a clean way to do a GET that returns json and then serializes it to a POJO.

As an example:

RestTemplate restTemplate = new RestTemplate();

// Add Jackson JSON Message Converter to Template
    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 that perfection can be tricky.

I found it useful to have small request / response classes for each call on the Android side, such as SaveNoteToServerRequest, SaveNoteToServerResponse classes, which are just Java objects with whatever fields are required 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 a response object.

On the PHP side, you can create a small class for the response object and then json_encode at the end.

So you cannot directly manipulate JSON objects just by using native plain Java objects or php classes most of the time.

Hope it helps.



All Articles