Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Makes a request to Imgur API
- public void MakeRequest(){
- var request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/gallery/hot/viral/0.json");
- request.Headers.Add ("Authorization", "Client-ID " + "920c7e989ed4deb");
- request.Method = "GET";
- Task<WebResponse> task = Task.Factory.FromAsync (
- request.BeginGetResponse,
- asyncResult => request.EndGetResponse (asyncResult),
- (object)null);
- task.ContinueWith (t => ReadStreamFromResponse (t.Result));
- }
- // Read the stream response, parse it to JSON, get the image urls ignoring the albums
- private void ReadStreamFromResponse(WebResponse response){
- using (Stream responseStream = response.GetResponseStream ()) {
- using (StreamReader sr = new StreamReader (responseStream)) {
- string content = sr.ReadToEnd ();
- var json = JsonObject.Parse (content);
- var array = json ["data"];
- List<string> urls = new List<string> ();
- foreach (JsonObject o in array) {
- string url = o ["link"];
- bool isAlbum = o ["is_album"];
- if (!isAlbum) {
- url = url.Insert (url.Length - 4, "s");
- urls.Add (url);
- }
- }
- totalPhotos = urls.Count;
- foreach (var url in urls) {
- AddElement (url);
- }
- }
- }
- }
- // For each image url, get the data, place it in a UIImage and add it to collectionData,
- // then assign it to the CollectionSource's photos List object and call ReloadData
- public void AddElement(string url){
- using (var imgUrl = new NSUrl (url)) {
- using (var data = NSData.FromUrl (imgUrl)) {
- collectionData.Add (UIImage.LoadFromData (data));
- downloadedPhotos++;
- if (downloadedPhotos == totalPhotos) {
- Console.WriteLine ("Finished downloading all photos");
- InvokeOnMainThread (delegate {
- photoSource.photos = collectionData;
- PhotoCollection.ReloadData ();
- Console.WriteLine("number of photos now "+ photoSource.photos.Count);
- });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement