import java.awt.TextField; import java.net.*; public class GUICatch extends PApplet implements ActionListener{ TextField myText = new TextField(); boolean waiting = false; void setup(){ size(200,200); myText.setLocation(10,100); myText.setSize(100,20); myText.addActionListener(this); add(myText); } void draw(){ myText.setLocation(10,100); //this is an oddity of processing that you have to set these over again in the draw loop myText.setSize(100,20); if (waiting){ background(random(100)); } } void actionPerformed(ActionEvent e) { //this is the listener function that is called by the text field waiting = true; HttpConnection conn = new HttpConnection(myText.getText()); //make my new connection object conn.start(); //start the thread myText.setText(""); } void netEvent(String _data, String _url){ //this gets called asynchronously from the httpConnection class if (_data != null){ println("came back" + _data); }else{ println("PROBLEM" + _url); } waiting = false; } class HttpConnection extends Thread{ //this class is a replacement for loadStrings //it is a thread so it plays nice and lets other things (like the draw function) to operate at the same time //it calls back to the NetEvent function String urlString; HttpConnection(String _url){ urlString = _url; println("new"); } public void run(){ //in a thread this is where you put the stuff you want to run conncurrently with other stuff println("ask"); try{ if (urlString.startsWith("http://") == false) urlString = "http://" + urlString ; URL url = new URL( urlString); // Create the URL InputStream in = url.openStream(); // Now copy bytes from the URL to the output stream BufferedReader br = new BufferedReader (new InputStreamReader(in)); String returnedText = ""; println("waiting"); while(true) { String aLine = br.readLine(); if (aLine == null) break; returnedText = returnedText + aLine; } netEvent(returnedText,urlString); br.close(); } catch (MalformedURLException dde) { netEvent(null,"Bad URL" + urlString + dde); //SEND BACK NULL IF THERE IS A PROBLEM } catch (Exception e) { netEvent(null,"Bad " + urlString + e); } } } }