HTTP Android
in
Android Processing
•
2 years ago
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:
- // Android fonts
- String[] fontList;
- PFont androidFont;
- Aclient html;
- void setup() {
- html = new Aclient();
- size(480, 640, A2D);
- orientation(PORTRAIT);
- fontList = PFont.list();
- androidFont = createFont(fontList[0], 12, true);
- textFont(androidFont);
- }
- void draw() {
- try{
- String data = html.executeHttpGet();
- text(data);
- }
- catch (java.lang.Exception JLE) {
- println("JLE: "+JLE);
- exit();
- }
- noLoop();
- }
And in second tab called "Aclient"
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URI;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- public class Aclient {
- public String executeHttpGet() throws Exception {
- BufferedReader in = null;
- try {
- HttpClient client = new DefaultHttpClient();
- HttpGet request = new HttpGet();
- request.setURI(new URI("http://www.processing.org/"));
- HttpResponse response = client.execute(request);
- in = new BufferedReader
- (new InputStreamReader(response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String page = sb.toString();
- System.out.println(page);
- return page;
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
1