处理中的Twitter应用程序

时间:2013-04-01 23:34:34

标签: api twitter processing

我正在尝试创建一个Twitter应用程序的大问题。我一直在这个问题上连续工作了2个星期,每天14个小时,我无法理解如何修复。我是新手,所以我确信这只是一件简单的事情。

我需要我的Twitter流一次显示五条推文。然后我需要删除最旧的推文并用新的推文替换它。现在我被告知最好的方法是使用Linkedlist,这非常有意义。但是,每当我尝试合并它时,它就行不通。

如果有人可以帮助我,我会无休止地感激,因为我现在已经知道了。

以下是代码:

import com.francisli.processing.http.*;

import java.util.LinkedList;

//LinkedList myList;


HashMap overall = new HashMap();


HttpClient client;

LinkedList myList;



PImage prhomoPix;

int results_to_show = 5;
int updateTime = 10000;
int updateDiff = 0;



void setup()
{

 myList = new LinkedList();
 for (int i = 0; i < 5; i++) 
 {
   myList.add(client);
 }

  size(1143, 800);



  prhomoPix = loadImage("PrhomoAppBg.jpg");

  background(0);
  image(prhomoPix, 0, 0);


  fill(0, 0, 0, 80);
  noStroke();
  rect(20, 135, 370, 670);

  fill(0, 0, 0, 80);
  noStroke();
  rect(755, 135, 370, 670);

  textAlign(CENTER, CENTER);

}






void responseReceived(HttpRequest request, HttpResponse response)
{


 if(response.statusCode == 200)
 {

    JSONObject allresults;
    allresults = response.getContentAsJSONObject();
    //JSONObject timeline_tweets = response.getContentAsJSONObject();






  for (int i = 0; i < 5; i++)
 {

    text(allresults.get("results").get(i).get("text").stringValue(), 25, 50+(i*120), 330, 330);

  }
  frameRate(2);




//}



   //for (int i=0; i < results_to_show; i++)
  //{

  // text(allresults.get("results").get(i).get("text").stringValue(), 25, 50+(i*120), 330, 330);
 // }

}

      else
  {
    text("UH-OH" + response.getContentAsString(), 50, 50);
  } 
}


void tweetUpdate()
{

  if(millis() > (updateTime + updateDiff))
  {


    client = new HttpClient(this, "search.twitter.com");//what webservice we are using, this is the priate OAuth one. "this" means it is not cpying setting from anywhere else
    client.GET("search.json?q=dublin&rpp="+results_to_show+"&result_type=recent");





    //println(myList);

    updateDiff = millis();

  }  

 }




   void draw()
{
 myList.remove(client);
 myList.add(client);
 tweetUpdate(); 

}

再次非常感谢!

2 个答案:

答案 0 :(得分:0)

老实说,Java真的不是我的领域,但这些教程看起来非常自我解释。希望他们能帮忙。

http://examples.javacodegeeks.com/core-java/util/linkedlist/

答案 1 :(得分:0)

  1. 您必须使用draw()方法重新绘制整个屏幕。
  2. 我想,有人告诉你要在LinkedList中保存推文,而不是HttpClient
  3. responseReceived()方法更新推文列表。
  4. 最终,你的抽奖看起来像这样:

    void draw() {
      // repaint
      background(0);
      // draw picture
      image(prhomoPix, 0, 0);
      // repaint all tweets
      for(int i=0; i< tweetsList.size(); ++i {
        drawTweet(tweetsList);
      }
      tweetUpdate(); // update list of tweets from server
    }
    

    tweetUpdate()

    void tweetUpdate() {
        client = new HttpClient(this, "search.twitter.com");
        client.GET("search.json?q=dublin&rpp="+results_to_show+"&result_type=recent");
    }