Android app to read JSON from website

I'm trying to start off my Android app journey with an app to display information from a website. The website is from a power draft system that helps me monitor temperatures of the fire and pieces of meat that I'm cooking for long periods of time on my smoker.

The JSON page is found at http://192.168.101.101/stoker.json on my local network and the data looks like this:

{
"stoker": {
    "sensors": [
        {
            "id": "620000116F01CA30",
            "name": "SS2",
            "al": 0,
            "ta": 66,
            "th": 75,
            "tl": 65,
            "tc": 69.1,
            "blower": null
        },
        {
            "id": "E20000116F0CDB30",
            "name": "brskt2",
            "al": 0,
            "ta": 203,
            "th": 32,
            "tl": 32,
            "tc": 71.7,
            "blower": null
        }

The code that I'm trying to use kept failing, so I added a log to the first step of the process to see what was being found. Here is the code:

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.101.101/stoker.json");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream webs = entity.getContent();

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),4);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }

    webs.close();

    result=sb.toString();
    Log.i("log_tag", result);

Here is the result from log during debug:

09-10 08:44:58.375: I/log_tag(748): <body>
09-10 08:44:58.375: I/log_tag(748): <h1>415 Unsupported Type</h1>
09-10 08:44:58.375: I/log_tag(748): </body>

I thought that reading the JSON information would be pretty straightforward, but it obviously isn't. What am I missing here?

At this point I would just like to display the information on my phone and nothing more. Down the road I would like to add more features to the app such as posting changes back to the JSON site and local alarms, but not right now.

Answers


It looks like a server-side error. What you can try is to change your HttpPost request to a HttpGet one.

If this doesn't work, then maybe your server is waiting for some specific data in the request header (Sometimes, the server wait for a specific User-agent field, or langage...)

Can you show us the server-side code which is generating this JSON ?


HTTP Status 415 - Unsupported Media Type in your case indicates that the server does not support the requested method for the requested resource, because you are trying to use the HTTP POST method instead of GET.

The full definition from the specification is:

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method

So I think you should be using HTTPGet instead. That said, you should really try to use a library that supports binding of JSON objects to Java classes. There are many libraries that encapsulate this functionality which should make your code a lot less complex since you are relying on a (hopefully well-tested) library.


Need Your Help

Where to instantiate interface in Nunit test

c# asp.net asp.net-mvc-2 nunit

I think theres something really simple I'm missing so I apologize in advance. I'm trying to test an interface with Nunit. The interface is implemented by a class derived from a base class and I'm...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.