Wednesday, December 9, 2009

Tweet from Java

Here's how you can use Twitter API to Tweet from java:


public class Tweet {
static String twitterUrl="";

public static void main(String[] args) throws Exception {
//Hate args checking.. Won't do it; Not even usage shall be displayed - figure it out
OutputStreamWriter ostreamwriter;
String statusmsg="";
int i=2;

URL url = new URL(twitterUrl);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + Base64.encode((args[0]+":"+args[1]).getBytes()));
while(i < args.length-1){
statusmsg = statusmsg+ args[i++]+" ";
statusmsg = statusmsg+ args[i];
String encStatus = "status="+URLEncoder.encode(statusmsg, "UTF-8");
ostreamwriter = new OutputStreamWriter(conn.getOutputStream());
ostreamwriter.write(encStatus); //--> Magic Magic :)
ostreamwriter.flush();                              //I forgot this at first..

//If interested you can check the response after this, though
//Won't bother with exceptions either. 
//A programmer should be intelligent enough to discover errors without any display mechanisms...

And voilĂ :

(or you could also use this )

1 comment:

  1. Thingiverse also strives guarantee that|to ensure that} all the 3D models introduced right here are|listed beneath are} licensed underneath their own artistic commons license. In fact, this is the guiding spirit behind maintaining this open platform. This means that just about|that almost|that virtually} anybody can either use or alter the models, designs and free STL information present here. This is the world’s largest 3D printing community and its Teapots primary declare to fame is that it firmly believes that the 3D printing world just isn't mutually unique. This is why it actively encourages everyone to both create and remix 3D models and other STL information. While many models can be found to purchase, a lot of|there are many} free models and STL object information could get} at no cost.