Android - API

Hi guys, I’m new (totally beginner) at Openhab. I had create android app for smarthome application and I want to connect it to Openhab via JSON/REST from scratch. Anyone can share me ideas how to connect it? Well i dont know how to start it, right now I just collecting piece by piece from this forum. please guide me. thanks guys
Fajar

Maybe this could help you.

Its the source code for the official openhab android app “HABdroid”.

thanks chris, that really helpfull. :smiley:

Hi @Fajar_D_Sutera,

If you just need a development environment (and not full blown OpenHAB setup of your own), I would download and install the runtime & demo files. That will get you going locally with something like this. Then, its basically just figuring out how to work with around OpenHAB REST API in your custom code. Generally simple GETs/POSTs to GET/SET values are used as needed. You may also need to study & understand how sitemaps and items are defined in OpenHAB as well - some mobile developers have created apps that expect a sitemap specific to their mobile application.

And as @christoph_wempe suggested, the HABDroid code base would be an excellent resource too!

Hi @d0t,

in fact I 've been able to install openhab on the server like this :slight_smile: Yes, I just want to make the output of a server in the form of GET / POST also with JSON format ( others are fine).

it is unfortunate that I cant make JSON format from server that can be captured by Android . I still have much to learn again (totally blank :frowning: ). thanks for sharing.

have you ever tried it?

From recollection OH server responses are always just plain text when doing a GET against an item, so you will likely need to construct the JSON objects yourself using the item/value pair.

EDIT - above statement is incorrect, you can specify ‘type’ in the URL like so: http://demo.openhab.org:8080/rest/items?type=json

Maybe if you share some further details of what you are building others can chime in with further details. For instance, is this a native Android application, or are you using a hybrid framework?

oh i see. so Openhab can also display text responses, I thought openhab just displays the existing UI from the modified sitemap. I 'm trying OHEditor right now, maybe I could showing the responses from it. Wow, thanks @d0t,

I created Native android application, Actually, I want to change to OH from my past server (window server) :smiley: .

Utilizing the REST API you can get the plain text values for any item. And actually it IS possible to specify JSON. For example:

http://OHSERVER:OHPORT/rest/items/Light_Group_Switch_All/state?type=json

In my case the endpoint above returns simply ‘ON’ in plain text, since there are no other values of concern. But if you wanted to explore ALL available items, for example, then both of these work too:

http://OHSERVER:OHPORT/rest/items?type=xml (xml is the default!)
http://OHSERVER:OHPORT/rest/items?type=json
http://demo.openhab.org:8080/rest/items?type=json

This returns a nested object in the requested format containing all of the OH items & metadata.

You should; will probably never look back :slight_smile: OpenHAB is awesome - fully open source, no vendor lock-ins, and works with practically everything out there! I’ve been using it for years and its flexibility and versatility is ‘bar-none’ :sunglasses:

All the best,
.