使用Stacky API获取Stack Overflow问题

时间:2013-04-24 14:08:27

标签: c# stackexchange-api stacky

我希望使用Stacky C# libraryStack Exchange API Stack Overflow 中获取最新的问题。

我拿了示例代码并尝试运行它,但是当它从 Stack Exchange 网站返回数据时它会挂起。

StackyClient client = new StackyClient("0.9", "", Sites.StackOverflow, 
            new UrlClient(), new JsonProtocol());

var o = new QuestionOptions();
o.FromDate = DateTime.Now.AddMinutes(-10.0);
o.ToDate = DateTime.Now;
o.IncludeAnswers = false;
o.IncludeBody = false;
o.IncludeComments = false;
o.SortBy = QuestionSort.Creation;
o.SortDirection = SortDirection.Descending;

IPagedList<Question> l = client.GetQuestions(o); <--- program hangs here 4ever

我做错了什么?

我还看到我可以register my application获取 API密钥。但是,没有必要让它一开始就运行,是吗?

修改

如果删除行

o.FromDate = DateTime.Now.AddMinutes(-10.0);
o.ToDate = DateTime.Now;

它起作用并返回所有问题。如果我添加行

o.Max = 50;

相反,它也不起作用。

编辑2

现在可行 - 重启我的电脑 顺便说一下,我最后使用了那段代码

var o = new QuestionOptions();
o.FromDate = DateTime.UtcNow.AddMinutes(-20);
o.IncludeAnswers = false;
o.IncludeBody = false;
o.IncludeComments = false;
o.SortBy = QuestionSort.Creation;
o.SortDirection = SortDirection.Descending;

IPagedList<Question> l = client.GetQuestions(o);

并且

o.Max

需要一个Unix纪元时间,而不是一些最大帖子。

2 个答案:

答案 0 :(得分:4)

尝试将StackyClient构造函数中指定的版本从“0.9”更改为“1.1”。当版本为“0.9”时,我在client.GetQuestions(o)行上收到JSON解析错误,但它在“1.1”下运行良好。

答案 1 :(得分:0)

使用bitbucket中的最新Stacky代码,GetQuestions不再有QuestionOptions参数。同样使用API​​的0.9版会导致Stacky崩溃,但根据this版本1.x已被弃用,所以可能会删除0.9吗?

  StackyClient client = new StackyClient("2.1", Sites.StackOverflow,
        new UrlClient(), new JsonProtocol());
  //var o = new QuestionOptions();
  //o.FromDate = DateTime.Now.AddMinutes(-10.0);
  //o.ToDate = DateTime.Now;
  //o.IncludeAnswers = false;
  //o.IncludeBody = false;
  //o.IncludeComments = false;
  //o.SortBy = QuestionSort.Creation;
  //o.SortDirection = SortDirection.Descending;
  QuestionSort sort = QuestionSort.Creation;
  SortDirection sortDir = SortDirection.Descending;
  int page = 1;
  int pageSize = 100;
  DateTime fromDate = DateTime.Now.AddMinutes(-10.0);
  DateTime toDate = DateTime.Now;
  IPagedList<Question> l = client.GetQuestions(sort, sortDir, page, pageSize, fromDate, toDate);
  foreach (var question in l)
  {
    Console.WriteLine(question.Title);
  }

或者,只需删除日期,看看是否有任何结果。

  IPagedList<Question> l = client.GetQuestions(sort, sortDir, page, pageSize);//, fromDate, toDate);
  foreach (var question in l)
  {
    Console.WriteLine(question.Title);
  }