I am working on a sensor app in android and I've to store the accelerometer readings on a django server and then retrieve them on my device. I am new to django and I don't know how to communicate with Android's HttpClient and a django server.
I want to write a django application that can respond to the HttpClient requests from an android device. Can I have an example code?
169 views Asked by Anjul Tyagi At
2
There are 2 answers
0
On
I believe the simplest way is using Django Rest Framework.
In this scenario, the steps are:
- Install Django.
- Create the the Django App and the Model for the data you want to store and access through the API.
- Setup Django Rest Framework. There are many tutorials that can help you here, for example Getting Started with Django Rest Framework and AngularJS .
- Access your new API from a browser and your Django App. Here is going to be very useful the browser Django Rest Frameworks will automagically create for your API.
Unless you need to do something 'special', Django Rest Framework will help you a lot, generating views, dealing with JSon serialization and so on. Basically, the only thing you will need to care is the model definition.
From the Android side, you will need:
Access your API using HttpClient. However, HttpClient library it is not recommended anymore to use it, as Google is focusing their efforts on improving the HttpURLConnection implementation.
Parser the data you got, in this in JSon format. To do this, you can use JsonReader.
Another alternative is using Retrofit, a very popular REST client for Android and Java, which solves the two problems very easily.
Django doesn't care what the client is, and Android's HttpClient don't care whether the url are served by Django, Tomcat, Rails, Apache or whatever. It's only HTTP. IOW: