Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private ArrayList ParseResponse(string html)
- {
- // Get the index of the first result
- int resultIndex = html.IndexOf("\"results\":[{\"created_at\":");
- // Check if there is no result (no result(s) shows "results:[]" and thus index == -1)
- if (resultIndex == -1) return null;
- // Take apart the results from the full json
- html = html.Substring(resultIndex + 11);
- // ArrayList to store individual results
- ArrayList results = new ArrayList();
- // Bool to indicate wheter there are more results left in the json
- bool moreResults = true;
- // Split the individual results
- do
- {
- resultIndex = html.IndexOf("},{\"created_at\":");
- if (resultIndex == -1)
- {
- moreResults = false;
- results.Add(html);
- }
- else
- {
- results.Add(html.Substring(0, resultIndex));
- html = html.Substring(resultIndex + 2);
- }
- } while (moreResults);
- // Save the tweets in an ArrayList
- ArrayList tweets = new ArrayList();
- // Make new tweet object of the results and put them in the ArrayList
- foreach (string result in results)
- {
- // User
- int userIndex = result.IndexOf("\"from_user\":\"") + 13;
- int userEndIndex = result.IndexOf(",\"from_user_id\":");
- string user = result.Substring(userIndex, userEndIndex - userIndex - 1);
- // Text
- int textIndex = result.IndexOf("\"text\":\"") + 8;
- int textEndIndex = result.IndexOf(",\"to_user\":");
- string text = result.Substring(textIndex, textEndIndex - textIndex - 1);
- // @todo ID
- tweets.Add(new Tweet()
- {
- Text = text,
- User = user
- });
- }
- // Return results
- return tweets;
- }
- /// <summary>
- /// Performs a HTTP GET WebRequest.
- /// </summary>
- /// <param name="url">The URL which needs to be requested.</param>
- /// <returns>The WebResponse of the request.</returns>
- private string PerformGetRequest(string url)
- {
- HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
- request.KeepAlive = false;
- WebResponse response = request.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- string responseString = reader.ReadToEnd();
- reader.Close();
- return responseString;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement