Saturday, April 11, 2015

Read dweet.io JSON using Java, develop and run on Raspberry Pi

I have a previous exercise of "Python to send data to Cloud". It can be viewed on browser at: http://dweet.io/follow/helloRaspberryPi_RPi2_vcgencmd.


Or read the dweets in JSON at: https://dweet.io/get/dweets/for/helloRaspberryPi_RPi2_vcgencmd
(Note that dweet.io only holds on to the last 500 dweets over a 24 hour period. If the thing hasn't dweeted in the last 24 hours, its history will be removed.)


Here is a Java exercise to parse the dweets JSON, develop and run on Raspberry Pi 2 with Netbeans IDE. Suppose it can run on any other Java SE supported platform.

JavaDweetIO.java
package javadweetio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JavaDweetIO {

    public static void main(String[] args) {
        
        String myThing = "helloRaspberryPi_RPi2_vcgencmd";
        try {
            JSONObject json = 
                ReadJSON("http://dweet.io/get/dweets/for/" + myThing);

            JSONArray jsonArray_with = json.getJSONArray("with");
            
            for(int i=0; i<jsonArray_with.length(); i++){
                JSONObject jsonObject_with = (JSONObject) jsonArray_with.get(i);
                String created = jsonObject_with.getString("created");
                JSONObject jsonObject_with_content = 
                    jsonObject_with.getJSONObject("content");
                Double RaspberryPi2_core_temp = 
                    jsonObject_with_content.getDouble("RaspberryPi2_core_temp");
                
                System.out.println(created);
                System.out.println(RaspberryPi2_core_temp);
                System.out.println("-----");
            }
            
        } catch (IOException | JSONException e){
            System.out.println(e.toString());
        }
    }

    public static JSONObject ReadJSON(String url) 
            throws IOException, JSONException {
        
        try (InputStream inputStream = new URL(url).openStream()) {
            InputStreamReader inputStreamReader = 
                new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            BufferedReader bufferedReader = 
                new BufferedReader(inputStreamReader);

            StringBuilder jsonBody = new StringBuilder();
            int singleChar;
            while ((singleChar = bufferedReader.read()) != -1) {
                jsonBody.append((char)singleChar);
            }

            JSONObject json = new JSONObject(jsonBody.toString());
            return json;
        }
    }
}


To import org.json in our Java code, we have to Add org.json library, java-json.jar, to NetBeans Java project.




If you want check the JSON online, before you parse it, you can try: http://jsonlint.com/



No comments: