At first I'm auditioning if I can communicate right with the Wikipedia API. My first goal is to get data from the API, turn it into an object, and display something on the screen that demonstrates that this process is correct.
I created a package called WikipediaViewer and placed a class named WikipediaMapProxy, which has a method whose purpose is to make the request, get the json and transform it into a C # object. In addition, within the WikipediaViewer package I have put several classes that will be used to create the object, resulting from json.
My questions:
1) In my MainPage.xaml.cs, you are giving a null pointer on line 33:
This means that there is a problem when picking json and transforming it into an object. In WikipediaMapProxy, I am using the DataContractJsonSerializer class that does the job of transforming json into the C # object. So I put [DataContract] on top of each class and [DataMember] on top of each property, to tell the DataContractJsonSerializer how it should do this transformation. However, the attribute names are not exactly as they are in json. For example, in json, there is no Item, because there they are named by a numbering, but I decided to call item. Also, the Pages class was meant to contain multiple items, so I put a list of items inside. I do not know if this is correct.
2) There is a problem on line 23: link
Encoding.UTF8.GetBytes () gets a string as a parameter, but if I put only result as a parameter, the compiler informs that it can not do an implicit conversion from a Task to a string, so I decided to put it as parameter result.Result, but I do not know if this is correct. What do you think?
See the Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace WikipediaViewer
{
public class WikipediaMapProxy
{
public async static Task<RootObject> GetResult(string searchedString)
{
string jsonSource = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=2&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=gandhi&origin=*";
var http = new HttpClient();
var response = await http.GetAsync(jsonSource);
var result = response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result.Result));
var data = (RootObject) serializer.ReadObject(ms);
return data;
}
}
[DataContract]
public class Thumbnail
{
[DataMember]
public string source { get; set; }
[DataMember]
public int width { get; set; }
[DataMember]
public int height { get; set; }
}
[DataContract]
public class Item
{
[DataMember]
public int pageid { get; set; }
[DataMember]
public int ns { get; set; }
[DataMember]
public string title { get; set; }
[DataMember]
public int index { get; set; }
[DataMember]
public Thumbnail thumbnail { get; set; }
[DataMember]
public string pageimage { get; set; }
[DataMember]
public string extract { get; set; }
}
[DataContract]
public class Pages
{
[DataMember]
public List<Item> item { get; set; }
}
[DataContract]
public class Query
{
[DataMember]
public Pages pages { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Query query { get; set; }
}
}