使用json数据从httpwebrequest统一检索图像

时间:2019-07-01 09:41:23

标签: c# json unity3d httpwebrequest

我正在构建一个增强现实应用程序,其中涉及从互联网检索的图像。

我发现了一个类似的基本应用程序,它使用了“免费”,并且效果很好。您输入搜索字词,它会返回该搜索中的所有图像,类似于google图片搜索。

下面的代码。

我不需要搜索功能,我只需要一个带有类别的按钮,当选中该按钮时,下一个场景将显示网站上所有图像的结果。

网站的示例是https://www.castlefineart.com/artists/robert-oxley#art

任何帮助将不胜感激。正如我所提到的,下面是免费代码。

正如我所说,我不需要搜索功能,只需要从特定页面加载所有图像即可。

欢呼

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;


/*
 * @author Michael Hässig
 * @email michael.haessig (at) gmail (dot) com
 */
public static class PixabayApi
{
    public static string URL = "https://pixabay.com/api/";

// My API KEY - the api is public but please do not abuse 
public static string KEY = "11701417-557ad1ed3c54b52741fa32e5c";

public static PixabayImageResponse Search(string search)
{
    // create http request
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + $"? 
 key={KEY}&q={search}");
    // set http method
    request.Method = "GET";
    // send request and auto close stream
    using (HttpWebResponse response =         
(HttpWebResponse)request.GetResponse())
    {
        // create reader for response stream
        StreamReader reader = new 
StreamReader(response.GetResponseStream());
        // read the full response into a string
        string jsonResponse = reader.ReadToEnd();
        // parse string into response model object
        PixabayImageResponse pixabayImageResponse =         
JsonUtility.FromJson<PixabayImageResponse>(jsonResponse);
        // return result
        return pixabayImageResponse;
       }
   }

}

[Serializable]
public class PixabayImage
{
public int id;
public string pageURL;
public string type;
public string tags;
public string previewURL;
public int previewWidth;
public int previewHeight;
public string webformatURL;
public int webformatWidth;
public int webformatHeight;
public string largeImageURL;
public string fullHDURL;
public string imageURL;
public int imageWidth;
public int imageHeight;
public int imageSize;
public int views;
public int downloads;
public int favorites;
public int likes;
public int comments;
public int user_id;
public string user;
public string userImageURL;
}

[Serializable]
public class PixabayImageResponse
{
public int total;
public int totalHits;
public List<PixabayImage> hits;
}

0 个答案:

没有答案