Mono: C# Twitter API Connections and XML
C# Twitter API Connections and XML:
In my last posts I discuss about consuming REST services in C#…Today I want to give an example about using Twitter API Connections in C#…
You can find all necessary information about API in: http://apiwiki.twitter.com/Twitter-API-Documentation
Here is the code:
/*
* Created by SharpDevelop.
* User: cakirh
* Date: 28.01.2010
* Time: 13:27
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using Gtk;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
namespace RESTserviceConn
{
class RESTserviceConn : Window
{
Gtk.Window window = new Gtk.Window("@tweet");
private Entry userName;
private Entry password;
private Entry tweetName;
private Label showText;
public RESTserviceConn()
: base("RESTserviceConn")
{
window.SetSizeRequest(500, 500);
window.SetPosition(WindowPosition.Center);
showText=new Label();
showText.SetSizeRequest(250,470);
showText.Wrap=true;
Image logo = new Gtk.Image("twitterapi_logo.png");
Button btnYahoo=new Button();
btnYahoo.Label="@myLastTWEET";
Label name=new Label();
name.Text="userName:";
userName=new Entry();
password=new Entry();
Label pss=new Label();
pss.Text="password:";
password.Visibility=false;
tweetName=new Entry();
Label twname=new Label();
twname.Text="tweerName:";
Button dd=new Button();
dd.Label="@getTWEETS";
Fixed fixedDownload=new Fixed();
fixedDownload.Put(logo,0,0);
fixedDownload.Put(name,30,40);
fixedDownload.Put(userName,30,60);
fixedDownload.Put(pss,30,80);
fixedDownload.Put(password,30,100);
fixedDownload.Put(twname,30,120);
fixedDownload.Put(tweetName,30,140);
fixedDownload.Put(btnYahoo,30,170);
fixedDownload.Put(dd,150,170);
fixedDownload.Put(showText,250,10);
btnYahoo.Clicked+= new EventHandler(btnYahoo_Clicked);
dd.Clicked+= new EventHandler(dd_Clicked);
window.Add(fixedDownload);
window.ShowAll();
}
void dd_Clicked(object sender, EventArgs e)
{
string[] gg = new string[20];
int i=0;
string target = "http://twitter.com/statuses/friends_timeline.xml";
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName.Text, password.Text);
try {
Stream stream = client.OpenRead(target);
StreamReader reader = new StreamReader(stream);
XmlTextReader xml_read = new XmlTextReader(stream);
while (xml_read.Read())
{
xml_read.MoveToElement();
if (xml_read.Name == "text")
{
gg[i]=xml_read.ReadInnerXml().ToString();
i++;
}
showText.Text=gg[0]+"\n\n"+gg[1]+"\n\n"+gg[2]+"\n\n"+gg[3]+"\n\n"+gg[4]+"\n\n"+gg[5]+"\n\n"+"\n\n"+gg[6]+"\n\n"+gg[7]+"\n\n"+"\n\n"+gg[8]+"\n\n"+gg[9];
}
} catch (WebException WebEx) {
Console.WriteLine("Error: " + WebEx.Message);
throw;
}
}
void btnYahoo_Clicked(object sender, EventArgs e)
{
string target = "http://twitter.com/users/"+tweetName.Text+".xml";
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName.Text, password.Text);
try {
Stream stream = client.OpenRead(target);
StreamReader reader = new StreamReader(stream);
XmlTextReader xml_read = new XmlTextReader(stream);
while (xml_read.Read())
{
xml_read.MoveToElement();
if (xml_read.Name == "followers_count")
{
MessageDialog md = new MessageDialog(window,
DialogFlags.DestroyWithParent,
MessageType.Info,
ButtonsType.Ok, "Followers: " + xml_read.ReadInnerXml().ToString());
md.Run();
md.Destroy();
}
else if (xml_read.Name == "text")
{
showText.Text=xml_read.ReadInnerXml().ToString();
}
}
} catch (WebException WebEx) {
Console.WriteLine("Error: " + WebEx.Message);
throw;
}
}
public static void Main()
{
Application.Init();
new RESTserviceConn();
Application.Run();
}
}
}
<pre>
Bear in mind that the code above done in very limited time so lots of improvements should be done about it…it is just a sample of how make calls by twitter api…
Outputs:
[1.] Getting tweets from followed users:
[2.]Getting last tweet of user:
Thats’ all folks for this post!
References:
[1.] http://www.infernodevelopment.com/c-twitter-api-connections-and-xml





failure 10:18 am on March 9, 2010 Permalink |
nice post, i needed windows forms one but this seems ok for me!