有没有办法在Processing中的draw()函数之外正确使用readString()?

时间:2013-09-24 00:19:36

标签: client processing

目前我有以下代码,一切都很好,除了我想从主机读取传入数据而不重复绘制功能。特别是因为我在编写的程序中不需要绘图功能。虽然循环似乎不起作用......

import processing.net.*;

String data;
Client c;
String hostvar;
String getReq;


void setup() {

  hostvar = "www.processing.org";  
  getReq = "/reference";

  c = new Client(this, hostvar, 80); // Connect to server on port 80

    //c.write("GET / HTTP/1.1\r\n"); // Use the HTTP "GET" command to ask for a Web page

  c.write("GET "+ getReq + " HTTP/1.1\r\n");
  c.write("Host:" + hostvar + "\r\n"); 
  c.write("User-Agent: Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.12; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7\n");
  c.write("\r\n");

  while (c.available() > 0) { // This code doesn't work. .
    data = c.readString(); // // This code doesn't work. .
    println(data); // This code doesn't work. .
  }// This code doesn't work. .
}

void draw() {

    if (c.available() > 0) { // If there's incoming data from the client...
      data = c.readString(); // ...then grab it and print it  //this code works...
      println(data);
    }
}

3 个答案:

答案 0 :(得分:1)

如果您只想获取数据:

void setup() {

  String hostvar = "http://www.processing.org/reference";
  println(join(loadStrings(hostvar),"\n"));
}

或者,如果您不想阻止/等待结果:

void setup() {

  new Thread() {
    public void run() {
      String hostvar = "http://www.processing.org/reference";
      println(PApplet.join(loadStrings(hostvar),"\n"));
    }
  }.start();

}

答案 1 :(得分:1)

最好的方法是使用Apache HttpComponents-Client。从here:下载最新的“二进制”zip文件。打开它,找到'lib'文件夹中的所有jar文件。然后按照here所述将这些直接拖到Processing IDE中。然后,您应该能够使用'examples'文件夹中的任何代码。这是一个简单的:

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

void setup() {

  CloseableHttpClient httpclient = HttpClients.createDefault();
  HttpGet httpGet = new HttpGet("http://targethost/homepage");
  CloseableHttpResponse response1 = httpclient.execute(httpGet);

  try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();

    // do something with the response body,
    // then ensure it is fully consumed

    EntityUtils.consume(entity1);
  } 
  finally {
    response1.close();
  }

}

答案 2 :(得分:1)

这也应该有用(如果你不想乱用任何jar),虽然你可能需要调整请求标题,使它看起来像你想要的任何“真正的”浏览器。

import java.io.*;
import java.net.*;

void setup() {
  String s = getHTML("http://www.processing.org/reference");
  println(s);
}

String getHTML(String url) {

  String line, result = "";

  try {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestMethod("GET");
    setRequestHeaders(conn);
    BufferedReader rd = new BufferedReader
      (new InputStreamReader(conn.getInputStream()));
    while ( (line = rd.readLine ()) != null) {
      result += line;
    }
    rd.close();
  } 
  catch (Exception e) {
    e.printStackTrace();
  }

  return result;
}

void setRequestHeaders(HttpURLConnection conn)
{
  String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
  conn.setRequestProperty("User-Agent", ua);
  conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
  conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
  conn.setRequestProperty("Connection", "keep-alive");
  conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
}