Loading...
Logo
Processing Forum
theboypip's Profile
2 Posts
14 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    Hi,

    I'm trying to port this functionality  to android and I couldn't find anything on the forums or by searching, other than that contributed library processing.net hadn't been ported yet?

    Basically I would like to get a string of xml returned from a twitter search so that I can do stuff with it (e.g count the number of tweets within 1 mile of the phone - using the phones gps or what ever).

    I started writing a post requesting help but found this  which slightly modified works as a class in Processing Android mode, see the following (written with a lot of help from MarkSelby) - it prints html in my phones screen. Thought the code might be useful to others.

    Oh and don't forget to turn INTERNET permission on.

    In first Tab:

    1. // Android fonts
    2. String[] fontList;
    3. PFont androidFont;

    4. Aclient html;

    5. void setup() {
    6.   html = new Aclient();
    7.   size(480, 640, A2D);

    8.   orientation(PORTRAIT);

    9.   fontList = PFont.list();
    10.   androidFont = createFont(fontList[0], 12, true);
    11.   textFont(androidFont);
    12. }

    13. void draw() {
    14.   try{
    15.     String data = html.executeHttpGet();
    16.     text(data);
    17.     }
    18.     catch (java.lang.Exception JLE) {
    19.       println("JLE: "+JLE);
    20.       exit();
    21.     }
    22.    noLoop();
    23.   }
    And in second tab called "Aclient"
    1. import java.io.IOException;
    2. import java.io.InputStreamReader;
    3. import java.net.URI;
    4. import org.apache.http.HttpResponse;
    5. import org.apache.http.client.HttpClient;
    6. import org.apache.http.client.methods.HttpGet;
    7. import org.apache.http.impl.client.DefaultHttpClient;
    8. public class Aclient {
    9.     public String executeHttpGet() throws Exception {
    10.         BufferedReader in = null;
    11.         try {
    12.             HttpClient client = new DefaultHttpClient();
    13.             HttpGet request = new HttpGet();
    14.             request.setURI(new URI("http://www.processing.org/"));
    15.             HttpResponse response = client.execute(request);
    16.             in = new BufferedReader
    17.             (new InputStreamReader(response.getEntity().getContent()));
    18.             StringBuffer sb = new StringBuffer("");
    19.             String line = "";
    20.             String NL = System.getProperty("line.separator");
    21.             while ((line = in.readLine()) != null) {
    22.                 sb.append(line + NL);
    23.             }
    24.             in.close();
    25.             String page = sb.toString();
    26.             System.out.println(page);
    27.             return page;
    28.             } finally {
    29.             if (in != null) {
    30.                 try {
    31.                     in.close();
    32.                     } catch (IOException e) {
    33.                     e.printStackTrace();
    34.                 }
    35.             }
    36.         }
    37.     }
    38. }


    I'm trying the Networking example and I'm getting strange results. Ultimately I simply want to return a twitter search query which there searchAPI says you can get with a simple HTTP GET on say  http://search.twitter.com/search.atom?q=Nottingham   and indeed you get a nice string of XML in a browser with this URL.

    However, I'm getting a strange return on www.processing.org. Running:

    1. import processing.net.*;

    2. Client c;
    3. String data;

    4. void setup() {
    5.   size(200, 200);
    6.   background(50);
    7.   fill(200);
    8.   c = new Client(this, "www.processing.org", 80); // Connect to server on port 80
    9.   c.write("GET / HTTP/1.1\r\n"); // Use the HTTP "GET" command to ask for a Web page
    10.   c.write("Host: 82.69.234.94\n\n"); // Be polite and say who we are
    11. }

    12. void draw() {
    13.   if (c.available() > 0) { // If there's incoming data from the client...
    14.     data = c.readString(); // ...then grab it and print it
    15.     println(data);
    16.   }
    17. }
    Returns


    HTTP/1.1 200 OK
    Date: Fri, 29 Apr 2011 11:10:00 GMT
    Server: Apache/2.2.14 (Ubuntu)
    Set-Cookie: cookiecookiecookie=82.69.234.94.1304075400820406; path=/; expires=Tue, 22-Apr-36 11:10:00 GMT; domain=.processing.org
    Last-Modified: Sat, 07 Aug 2010 18:10:10 GMT
    ETag: "830c90d-1cd-48d3fb1a9e480"
    Accept-Ranges: bytes
    Content-Length: 461
    Vary: Accept-Encoding
    Content-Type: text/html

    <html>

    <!-- why yes, this was written by hand! and yes, it is HTML 2.0. or worse. 
         but the more important question is... why are you reading this? -->

    <head> <title>
    android.processing.org
    </title> </head>

    <body>

    <center> <table width="500"> <tr> <td>
    <font face="verdana, sans-serif" size=1>

    <h3> Processing for Android </h3>

    This page has moved <a href="http://wiki.processing.org/w/Android">to the wiki</a>.

    </td> </tr> </table>

    </body>

    </html>
    WHich is not what I was expecting. And when I run 


    java.net.UnknownHostException: search.twitter.com/search.atom?q=Nottingham
    And a whole load more error lines.

    I'm behind a domestic router which has the external IP 82.69.234.94 and my laptop is on 192.168.2.17 behind that router. I've tried on processing 1.2.1 and dev 0194, same results. Running Ubuntu with java 6...