zoukankan      html  css  js  c++  java
  • 自制新浪微博客户端

    实现:

    1、新浪帐号的登录

    2、用户新浪微博头像显示

    3、用户新浪微博昵称、微博地址、所在地区、用户格言显示

    4、用户关注数、粉丝数、微博数显示

    5、文字微博发布

    6、实时跟新微博数

    7、实时备份新浪微博

     代码:

    登陆
     1    public string APP_KEY = "***";
     2         public string APPSECRET = "***";
     3         public string CALLBACKURL = "***";
     4         public System.Timers.Timer timer = new System.Timers.Timer();
     5         public OAuth oauth = null;
     6         public Client Sina = null;
     7         public Form1()
     8         {
     9             InitializeComponent();
    10         }
    11 
    12         private void button1_Click(object sender, EventArgs e)
    13         {
    14             if (string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
    15             {
    16                 MessageBox.Show("请输入微博帐号");
    17                 return;
    18             }
    19             if (string.IsNullOrEmpty(this.txtPwd.Text.Trim()))
    20             {
    21                 MessageBox.Show("请输入微博密码");
    22                 return;
    23             }
    24             oauth = Authorize(this.txtUserName.Text, this.txtPwd.Text);
    25             if (oauth == null)
    26             {
    27                 MessageBox.Show("登陆失败");
    28                 return;
    29             }
    30             content t = new content(oauth);
    31             t.Show();
    32             this.Hide();
    33         }
    34         public OAuth Authorize(string userId, string pwd)
    35         {
    36             OAuth o = new OAuth(APP_KEY, APPSECRET, CALLBACKURL);
    37             if (!o.ClientLogin(userId, pwd))
    38             {
    39                 return null;
    40             }
    41             else
    42             {
    43                 return o;
    44             }
    45 
    46         }
    47         private void button2_Click(object sender, EventArgs e)
    48         {
    49             Application.Exit();
    50         }
    51 
    52         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    53         {
    54             Application.Exit();
    55         }
    发布端
      1  OAuth oauth = null;
      2         Client sina = null;
      3         public string XML_PATH = ConfigurationSettings.AppSettings["xmlPath"].ToString();
      4         System.Timers.Timer timer = new System.Timers.Timer();
      5         public content()
      6         {
      7             InitializeComponent();
      8         }
      9         public content(OAuth o)
     10         {
     11             InitializeComponent();
     12             timer.Interval = 1000;
     13             timer.Enabled = true;
     14             timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
     15             oauth = o;
     16             sina = new Client(oauth);
     17             string uid = sina.API.Entity.Account.GetUID();
     18             var entity = sina.API.Entity.Users.Show(uid);
     19 
     20             this.label1.Text = entity.ScreenName;
     21             this.label2.Text = entity.Location;
     22             this.label3.Text = entity.Description;
     23             this.pictureBox1.ImageLocation = entity.ProfileImageUrl;
     24             this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
     25             this.label4.Text = "http://weibo.com/" + entity.ProfileUrl;
     26             long favourite = entity.FavouritesCount;//收藏数
     27             int followMeCount = entity.FollowersCount;//粉丝数
     28             int friendsCount = entity.FriendsCount;//关注数
     29             int statuseCount = entity.StatusesCount;//微博数
     30 
     31             this.label5.Text = string.Format("关注{0} 粉丝{1} 微博{2}", friendsCount, followMeCount, statuseCount);
     32             NetDimension.Weibo.Entities.status.Collection weibolist = sina.API.Entity.Statuses.UserTimeline(uid);
     33             DataTable dt = new DataTable();
     34             dt.Columns.Add("content", typeof(string));
     35             dt.Columns.Add("created_at", typeof(string));
     36             dt.Columns.Add("favorited", typeof(string));
     37             dt.Columns.Add("reposts_count", typeof(string));
     38             dt.Columns.Add("comments_count", typeof(string));
     39             dt.Columns.Add("id", typeof(string));
     40             dt.Columns.Add("mid", typeof(string));
     41             foreach (NetDimension.Weibo.Entities.status.Entity weibo in weibolist.Statuses)
     42             {
     43                 DataRow dr = dt.NewRow();
     44                 dr["content"] = weibo.Text;//内容
     45                 dr["created_at"] = weibo.CreatedAt;
     46                 dr["favorited"] = weibo.Favorited;
     47                 dr["reposts_count"] = weibo.RepostsCount;
     48                 dr["comments_count"] = weibo.CommentsCount;
     49                 dr["id"] = weibo.ID;
     50                 dr["mid"] = weibo.MID;
     51                 dt.Rows.Add(dr);
     52             }
     53             BindUserMsg(dt);
     54         }
     55 
     56         void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
     57         {
     58             string uid = sina.API.Entity.Account.GetUID();
     59             var entity = sina.API.Entity.Users.Show(uid);
     60             int followMeCount = entity.FollowersCount;//粉丝数
     61             int friendsCount = entity.FriendsCount;//关注数
     62             int statuseCount = entity.StatusesCount;//微博数
     63             this.label5.Text = string.Format("关注{0} 粉丝{1} 微博{2}", friendsCount, followMeCount, statuseCount);
     64         }
     65         /// <summary>
     66         /// 记录日志
     67         /// </summary>
     68         /// <param name="dt"></param>
     69         public void BindUserMsg(DataTable dt)
     70         {
     71             XmlTextWriter writer = new XmlTextWriter(XML_PATH, null);
     72             writer.WriteStartDocument();
     73             writer.WriteStartElement("root");
     74             if (dt == null)
     75             {
     76                 return;
     77             }
     78             for (int i = 0; i < dt.Rows.Count; i++)
     79             {
     80                 string id = dt.Rows[i]["id"].ToString();
     81                 string mid = dt.Rows[i]["mid"].ToString();
     82                 string content = dt.Rows[i]["content"].ToString();
     83                 string createAt = dt.Rows[i]["created_at"].ToString();
     84                 string favorited = dt.Rows[i]["favorited"].ToString();
     85                 string reposts_count = dt.Rows[i]["reposts_count"].ToString();
     86                 string comments_count = dt.Rows[i]["comments_count"].ToString();
     87                 writer.WriteStartElement("fields");
     88                 WriterFields(writer, "id", id);
     89                 WriterFields(writer, "mid", mid);
     90                 WriterFields(writer, "content", content);
     91                 WriterFields(writer, "createAt", createAt);
     92                 WriterFields(writer, "favorited", favorited);
     93                 WriterFields(writer, "reposts_count", reposts_count);
     94                 WriterFields(writer, "comments_count", comments_count);
     95                 writer.WriteEndElement();
     96             }
     97             writer.WriteEndElement();
     98             writer.WriteEndDocument();
     99             writer.Flush();
    100             writer.Close();
    101         }
    102         /// <summary>
    103         /// 写字段
    104         /// </summary>
    105         /// <param name="writer"></param>
    106         /// <param name="type"></param>
    107         /// <param name="value"></param>
    108         public void WriterFields(XmlTextWriter writer, string type,string value)
    109         {
    110             writer.WriteStartElement("field");
    111             writer.WriteStartAttribute("type");
    112             writer.WriteString(string.Format("{0}",type));
    113             writer.WriteEndAttribute();
    114             writer.WriteString(value);
    115             writer.WriteEndElement();
    116         }
    117         /// <summary>
    118         /// 发微博
    119         /// </summary>
    120         /// <param name="sender"></param>
    121         /// <param name="e"></param>
    122         private void button1_Click(object sender, EventArgs e)
    123         {
    124             if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
    125             {
    126                 MessageBox.Show("发布内容不能为空!");
    127                 return;
    128             }
    129             NetDimension.Weibo.Entities.status.Collection c = sina.API.Entity.Statuses.UserTimeline(sina.API.Entity.Account.GetUID());
    130             string strPreContent = "";
    131             string content = this.textBox1.Text.Trim();
    132             foreach (NetDimension.Weibo.Entities.status.Entity item in c.Statuses)
    133             {
    134                 strPreContent = item.Text;
    135                 break;
    136             }
    137             if (strPreContent.Equals(content))
    138             {
    139                 MessageBox.Show("不要太贪心哟,发一次就够啦!");
    140                 return;
    141             }
    142             sina.API.Entity.Statuses.Update(content);
    143             this.textBox1.Text = string.Empty;
    144             MessageBox.Show("发布成功");
    145         }
    146 
    147         private void content_FormClosing(object sender, FormClosingEventArgs e)
    148         {
    149             sina.API.Entity.Account.EndSession();
    150 
    151             Application.Exit();
    152         }
    153 
    154 
    155         private void content_Resize(object sender, EventArgs e)
    156         {
    157             if (this.WindowState == FormWindowState.Minimized)
    158             {
    159                 this.WindowState = FormWindowState.Normal;
    160                 notifyIcon1.Visible = true;
    161                 this.Hide();
    162                 this.ShowInTaskbar = false;
    163             }
    164         }
    165 
    166         private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
    167         {
    168             if (this.ShowInTaskbar == false)
    169             {
    170                 notifyIcon1.Visible = true;
    171                 this.ShowInTaskbar = true;
    172                 this.Show();
    173                 this.Activate();
    174                 this.WindowState = FormWindowState.Normal;
    175             }
    176         }

     下载地址

  • 相关阅读:
    精选 TOP 面试题
    leecode100热题 HOT 100(2)
    leecode100热题 HOT 100
    leetcode题库
    LeetCode All in One 题目讲解汇总(持续更新中...)
    LVS负载均衡(LVS简介、三种工作模式、十种调度算法)
    Smart/400开发上手3: 练习实践
    Smart/400开发上手2: COBOL批处理程序处理过程
    Smart/400开发上手1:入门
    一步步Cobol 400上手自学入门教程06
  • 原文地址:https://www.cnblogs.com/hfliyi/p/2857482.html
Copyright © 2011-2022 走看看