Skip to content

Commit 9cde9ec

Browse files
author
agoransson
committed
Added a new example parsing HTTP header. Removed the requirement for exception handling in HTTP.java.
Signed-off-by: agoransson <andreas.goransson@mah.se>
1 parent 3fe6da2 commit 9cde9ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+11899
-36
lines changed
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 2: Creating a JSON Array
4+
*
5+
* Good for sending a large set of primitive values, like sensor readings.
6+
*/
7+
8+
import org.json.*;
9+
10+
void setup(){
11+
12+
// 1. Initialize the Array
13+
JSONArray myJsonArray = new JSONArray();
14+
15+
// 2. Add some content to the array
16+
myJsonArray.put( 4 );
17+
myJsonArray.put( 2 );
18+
19+
println( myJsonArray );
20+
}
21+
22+
void draw(){
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 3: Creating a JSON Array of JSON Objects.
4+
*
5+
* Good for sending multiple complex values, such as database tables.
6+
*/
7+
8+
import org.json.*;
9+
10+
void setup(){
11+
12+
// 1. Initialize the Array
13+
JSONArray myJsonUsers = new JSONArray();
14+
15+
// 2. Create the first object & add to array
16+
JSONObject firstUser = new JSONObject();
17+
firstUser.put( "name", "Andreas" );
18+
firstUser.put( "age", 32 );
19+
myJsonUsers.put( firstUser );
20+
21+
// 3. Create the second object
22+
JSONObject secondUser = new JSONObject();
23+
secondUser.put( "name", "Maria" );
24+
secondUser.put( "age", 28 );
25+
myJsonUsers.put( secondUser );
26+
27+
println( myJsonUsers );
28+
}
29+
30+
void draw(){
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 1: Creating a JSON Object
4+
*
5+
* Good for sending values that has a specific meaning (complex values)
6+
*/
7+
8+
import org.json.*;
9+
10+
void setup(){
11+
12+
// 1. Initialize the object
13+
JSONObject myJsonObject = new JSONObject();
14+
15+
// 2. Add some content to the object
16+
myJsonObject.put( "myIntegerValue", 7 );
17+
18+
println( myJsonObject );
19+
}
20+
21+
void draw(){
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 5: Parsing the HTTP header from www.processing.org
4+
*
5+
* This example creates a JSONObject from the HTTP response when
6+
* contacting a website (www.processing.org).
7+
*/
8+
import org.json.*;
9+
import java.net.URL;
10+
import java.net.URLConnection;
11+
12+
URL url;
13+
URLConnection conn;
14+
15+
void setup() {
16+
// Unfortunately there's a problem in the Processing Client implementation
17+
// that forces us to use straight Java.
18+
try {
19+
url = new URL("http://www.processing.org");
20+
conn = url.openConnection();
21+
}
22+
catch( Exception e) {
23+
}
24+
25+
// We will manually add the entire HTTP reponse to this StringBuffer
26+
// and create the JSONObject using it.
27+
StringBuffer sb = new StringBuffer();
28+
29+
// Construct the String object using the URLConnection.
30+
for (int i = 0;; i++) {
31+
String name = conn.getHeaderFieldKey(i);
32+
String value = conn.getHeaderField(i);
33+
if (name == null && value == null) {
34+
break;
35+
}
36+
if (name == null) {
37+
// Add the value, if there is no key-value pair.
38+
sb.append(value).append("\n");
39+
}
40+
else {
41+
// Add the key-value pair.
42+
sb.append(name).append(":").append(value).append("\n");
43+
}
44+
}
45+
46+
// Create the JSON HTTP instance
47+
HTTP http = new HTTP();
48+
49+
// Create the JSONObject using the HTTP instance
50+
JSONObject obj = http.toJSONObject(sb.toString());
51+
52+
// Print the JSONObject
53+
System.out.println(obj);
54+
}
55+
void draw() {
56+
}
57+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 1: Creating a JSON Object from a json string
4+
*/
5+
6+
import org.json.*;
7+
8+
void setup(){
9+
10+
// 1. Get the json-string (we'll just create one...)
11+
String jsonstring = "{\"myIntegerValue\":7}";
12+
13+
// 2. Initialize the object
14+
JSONObject myJsonObject = new JSONObject(jsonstring);
15+
16+
println( myJsonObject );
17+
}
18+
19+
void draw(){
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 4: Reading data from the web (Yahoo weather)
4+
*
5+
* This example reads the Weather feed from the Yahoo API's. It's using
6+
* the WOEID for Malmo, you can change this by finding the ID you want
7+
* at http://developer.yahoo.com/weather/
8+
*/
9+
10+
import org.json.*;
11+
12+
void setup() {
13+
// Accessing the weather service
14+
String BASE_URL = "http://weather.yahooapis.com/forecastjson?w=";
15+
String WOEID = "898091";
16+
17+
// Get the JSON formatted response
18+
String response = loadStrings( BASE_URL + WOEID )[0];
19+
20+
// Make sure we got a response.
21+
if ( response != null ) {
22+
// Initialize the JSONObject for the response
23+
JSONObject root = new JSONObject( response );
24+
25+
// Get the "condition" JSONObject
26+
JSONObject condition = root.getJSONObject("condition");
27+
28+
// Get the "temperature" value from the condition object
29+
int temperature = condition.getInt("temperature");
30+
31+
// Print the temperature
32+
println( temperature );
33+
}
34+
}
35+
36+
void draw() {
37+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4+
<head>
5+
<title>json4processing</title>
6+
<meta name="description" content="a library for the programming environment processing" />
7+
<meta name="keywords" content="processing.org, library" />
8+
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
9+
<meta http-equiv="Content-Language" content="en-us" />
10+
<meta name="ROBOTS" content="index,follow,archive" />
11+
<meta http-equiv="imagetoolbar" content="false" />
12+
<meta name="MSSmartTagsPreventParsing" content="true" />
13+
<meta name="author" content="##author##" />
14+
<meta name="Rating" content="General" />
15+
<meta name="revisit-after" content="7 Days" />
16+
<meta name="doc-class" content="Living Document" />
17+
<link rel="stylesheet" type="text/css" href="./stylesheet.css">
18+
</head>
19+
<body>
20+
<div id="container">
21+
22+
<div id="header">
23+
<h1>json4processing</h1>
24+
</div>
25+
26+
<div id="menu" class="clear">
27+
<ul>
28+
<li><a href="#about">About</a> \ </li>
29+
<li><a href="#download">Download</a> \ </li>
30+
<li><a href="#download">Installation</a> \ </li>
31+
<li><a href="#examples">Examples</a> \ </li>
32+
<li><a href="./reference/index.html" target="_blank">Reference</a></li>
33+
<!-- <li><a href="#demos">Demos</a> \ </li> -->
34+
<!-- <li><a href="#misc">Misc</a> \ </li> -->
35+
<!-- <li><a href="#images">Images</a> \ </li> -->
36+
</ul>
37+
</div>
38+
39+
<div id="content" class="clear">
40+
41+
<div id="about">
42+
<h2>json4processing</h2>
43+
<p>
44+
A library by <a href="##yourLink##">Andreas Goransson</a> for the programming environment <a href="http://www.processing.org" target="_blank">processing</a>. Last update, 03/31/2012.
45+
</p>
46+
<p>
47+
Feel free to replace this paragraph with a description of the library. Contributed libraries are developed, documented, and maintained by members of the Processing community. Further directions are included with each library. For feedback and support, please post to the Discourse. We strongly encourage all libraries to be open source, but not all of them are.
48+
</p>
49+
</div>
50+
51+
52+
53+
<div id="download" class="clear">
54+
<h2>Download</h2>
55+
<p>
56+
Download json4processing version 0.1.3 in
57+
<a href="./download/json4processing-0.1.3.zip">.zip format</a>.
58+
</p>
59+
<h2>Installation</h2>
60+
<p>
61+
Unzip and put the extracted json4processing folder into the libraries folder of your processing sketches. Reference and examples are included in the json4processing folder.
62+
</p>
63+
</div>
64+
65+
66+
<div id="resources">
67+
<p><strong>Keywords</strong> ?</p>
68+
<p><strong>Reference</strong>. Have a look at the javadoc reference <a href="./reference/index.html" target="_blank">here</a>. a copy of the reference is included in the .zip as well.</p>
69+
<p><strong>Source</strong>. The source code of json4processing is available at <a href="https://github.com/agoransson/JSON-processing">github</a>, and its repository can be browsed <a href="https://github.com/agoransson/JSON-processing" target="_blank">here</a>.</p>
70+
</div>
71+
72+
<div id="examples" class="clear">
73+
<h2>Examples</h2>
74+
<p>Find a list of examples in the current distribution of json4processing, or have a look at them by following the links below.</p>
75+
<ul>
76+
<li><a href="examples/create_jsonarray/create_jsonarray.pde">create_jsonarray</a></li> <li><a href="examples/create_jsonarray_of_objects/create_jsonarray_of_objects.pde">create_jsonarray_of_objects</a></li> <li><a href="examples/create_jsonobject/create_jsonobject.pde">create_jsonobject</a></li> <li><a href="examples/create_jsonobject_from_httpheader/create_jsonobject_from_httpheader.pde">create_jsonobject_from_httpheader</a></li> <li><a href="examples/create_jsonobject_from_string/create_jsonobject_from_string.pde">create_jsonobject_from_string</a></li> <li><a href="examples/reading_yahoo_weather/reading_yahoo_weather.pde">reading_yahoo_weather</a></li>
77+
</ul>
78+
</div>
79+
80+
81+
<div id="info">
82+
<h2>Tested</h2>
83+
<p>
84+
<!-- on which platform has the library been tested? -->
85+
<strong>Platform</strong> linux, windows
86+
87+
<!-- which processing version did you use for testing your library? -->
88+
<br /><strong>Processing</strong> 1.5.1
89+
90+
<!-- does your library depend on any other library or framework? -->
91+
<br /><strong>Dependencies</strong> ?
92+
</p>
93+
</div>
94+
95+
96+
<!-- use the demos section for a list of applets run in a browser. -->
97+
<!--
98+
<div id="demos" class="clear">
99+
<h2>demos</h2>
100+
<p>
101+
find a list of online applet demos below.
102+
103+
<ul>
104+
<li><a href="./applets/demo/index.html">demo</a></li>
105+
</ul>
106+
</p>
107+
</div>
108+
-->
109+
110+
<!-- use the misc section for other relevant information. Activate the link to the misc section in the menu above. -->
111+
<!--
112+
<div id="misc" class="clear">
113+
<p></p>
114+
</div>
115+
-->
116+
117+
<!-- use the images/screenshots section. Activate the link to the misc section in the menu above. -->
118+
<!--
119+
<div id="images" class="clear">
120+
</div>
121+
-->
122+
123+
124+
<br class="clear" />
125+
</div>
126+
127+
<div id="footer">
128+
<p>by Andreas Goransson, (c) 2011.</p>
129+
</div>
130+
</div>
131+
</body>
132+
</html>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2+
<!--NewPage-->
3+
<HTML>
4+
<HEAD>
5+
<!-- Generated by javadoc (build 1.6.0_26) on Sat Mar 31 13:00:45 CEST 2012 -->
6+
<TITLE>
7+
All Classes (Javadocs: json4processing)
8+
</TITLE>
9+
10+
<META NAME="date" CONTENT="2012-03-31">
11+
12+
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
13+
14+
15+
</HEAD>
16+
17+
<BODY BGCOLOR="white">
18+
<FONT size="+1" CLASS="FrameHeadingFont">
19+
<B>All Classes</B></FONT>
20+
<BR>
21+
22+
<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
23+
<TR>
24+
<TD NOWRAP><FONT CLASS="FrameItemFont"><A HREF="org/json/CDL.html" title="class in org.json" target="classFrame">CDL</A>
25+
<BR>
26+
<A HREF="org/json/Cookie.html" title="class in org.json" target="classFrame">Cookie</A>
27+
<BR>
28+
<A HREF="org/json/CookieList.html" title="class in org.json" target="classFrame">CookieList</A>
29+
<BR>
30+
<A HREF="org/json/HTTP.html" title="class in org.json" target="classFrame">HTTP</A>
31+
<BR>
32+
<A HREF="org/json/HTTPTokener.html" title="class in org.json" target="classFrame">HTTPTokener</A>
33+
<BR>
34+
<A HREF="org/json/JSONArray.html" title="class in org.json" target="classFrame">JSONArray</A>
35+
<BR>
36+
<A HREF="org/json/JSONException.html" title="class in org.json" target="classFrame">JSONException</A>
37+
<BR>
38+
<A HREF="org/json/JSONML.html" title="class in org.json" target="classFrame">JSONML</A>
39+
<BR>
40+
<A HREF="org/json/JSONObject.html" title="class in org.json" target="classFrame">JSONObject</A>
41+
<BR>
42+
<A HREF="org/json/JSONString.html" title="interface in org.json" target="classFrame"><I>JSONString</I></A>
43+
<BR>
44+
<A HREF="org/json/JSONStringer.html" title="class in org.json" target="classFrame">JSONStringer</A>
45+
<BR>
46+
<A HREF="org/json/JSONTokener.html" title="class in org.json" target="classFrame">JSONTokener</A>
47+
<BR>
48+
<A HREF="org/json/JSONWriter.html" title="class in org.json" target="classFrame">JSONWriter</A>
49+
<BR>
50+
<A HREF="org/json/XML.html" title="class in org.json" target="classFrame">XML</A>
51+
<BR>
52+
<A HREF="org/json/XMLTokener.html" title="class in org.json" target="classFrame">XMLTokener</A>
53+
<BR>
54+
</FONT></TD>
55+
</TR>
56+
</TABLE>
57+
58+
</BODY>
59+
</HTML>

0 commit comments

Comments
 (0)