ListPage

2010年12月2日木曜日

[C#] Yahoo デベロッパーネットワーク 続き

Yahoo デベロッパーネットワーク

WebAPI

とりあえず、オークション情報の取得を考えた結果、前回の様なカテゴリー名やカテゴリーIDが
必要だった!

で、何も考えずチェックボックスのNameプロパティの後にカテゴリーIDを付け足してあった・・。

this.CategoryCheckBox[i].Name = "CategoryCheckBox" + dt.Rows[i][0].ToString();
・・・こっからどう進めて行こうか。
とりあえず、ちゃんとCategoryCheckBoxができてるか確かめー。


ううーむ・・。チェックを入れた時にカテゴリーIDを収集するか・・。
Listとかに突っ込んでKEYみたいので判断するか悩むなー。と適当な事を考えつつ
WEBを見てたら、昨日書いたデシリアライズとシリアライズのサンプル発見!!

  public Yahoo.API.WebSearchResponse.ResultSet WebSearch(string appId, string query, string type, short results, int start, string format, bool adultOk, bool similarOk, string language)
  {
   string requestUri = 
                String.Format("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid={0}&query={1}&type={2}&results={3}&start={4}&format={5}&adult_ok={6}&similar_ok={7}&language={8}", 
                appId, HttpUtility.UrlEncode(query, Encoding.UTF8) , type, results, 
                start, format, adultOk ? "1" : "0", similarOk ? "1" : "0", language);

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

   Yahoo.API.WebSearchResponse.ResultSet resultSet = null;
   using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
   {
    using (Stream responseStream = response.GetResponseStream())
    {
     XmlSerializer serializer = new XmlSerializer(typeof(Yahoo.API.WebSearchResponse.ResultSet));
     resultSet = (Yahoo.API.WebSearchResponse.ResultSet)serializer.Deserialize(responseStream);
    }
   }

   return resultSet;
  }

引数多すぎ!って思ったけどまぁ考えてくれた人ありがとー!この攻撃的なコードは
日本人じゃない。多分。

また駄文が長くなったので次回。

2010年12月1日水曜日

[C#] Yahoo デベロッパーネットワーク

Yahoo デベロッパーネットワーク

Web API

C#

激しく見づらいって言われたの加筆・修正

そういえば、とある質問サイトでYahooAPIなんてもんがあったなと
思い出し解答しましたが。実際には使った事なかったり・・。
なんてわけにもいかなかったので検索した所、PHPとかJAVAのサンプルはあったけど
.NETのサンプルは中々見つからなかったのでちょっとしこしこ書いてみた。

        public System.Data.DataSet ReturnCategory(int CategoryID)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            string ApiUrl = 
            string.Format("http://auctions.yahooapis.jp/AuctionWebService/V2/categoryTree" +
                                   "?appid={0}" +
                                   "&category={1}", ID, CategoryID);

            HttpWebRequest httpwebrequest =
                WebRequest.Create(ApiUrl) as HttpWebRequest;

            using (HttpWebResponse response = httpwebrequest.GetResponse() as HttpWebResponse)
            {
                ds.ReadXml(response.GetResponseStream());
            }
            return ds;
        }

こんな感じで、XMLでもDataSetでも返せる。本格的に作るんであればシリアライズして
クラス事に欲しい値を返せるようにするのが自分的なベストだけどサンプルでやるには
めんどすぎる為割愛ー。

でこんな感じで、カテゴリーの一覧を取得

まずは、guiをちょこっと作ってみた。

        //チェックボックスの配列の作成
        private CheckBox[] CategoryCheckBox;
        private void YahooSampleAPI_Load(object sender, EventArgs e)
        {
            //カテゴリーを決定する
            this.toolStripStatusLabel2.Text = "カテゴリーの取得中";
            int cid = 0;
            DataSet ds = yahooWebService.ReturnCategory(cid);
            DataTable dt = ds.Tables["ChildCategory"];
            int datacount = dt.Rows.Count - 1;
            //レイアウトの編集
            this.SuspendLayout();
            this.CategoryCheckBox = new CheckBox[datacount];
            for (int i = 0; i < datacount; i++)
            {
                this.CategoryCheckBox[i] = new CheckBox();
                this.CategoryCheckBox[i].Name = "CategoryCheckBox" + dt.Rows[i][0].ToString();
                this.CategoryCheckBox[i].Text = dt.Rows[i][1].ToString();
                this.CategoryCheckBox[i].AutoSize = true;
                int width = CategoryCheckBox[i].Width;
                this.CategoryCheckBox[i].Location = new Point(width + 15, 20);
                this.CategoryCheckBox[i].TabIndex = i;
                //this.flowLayoutPanel1.SetFlowBreak(CategoryCheckBox[i], true);
            }
            this.flowLayoutPanel1.Controls.AddRange(this.CategoryCheckBox);
            this.ResumeLayout(false);
            this.toolStripStatusLabel2.Text = "カテゴリーの取得に成功しました。";
        }

長くなったので次回.