我使用jsontocsharp在线转换器转换了两个api扩展。通过第一个API,类可用于通过按钮单击搜索特定书籍。在文本框中输入输入后,将显示项目列表。当用户点击书籍时,它将使用下载按钮导航到其他页面。第二个api课程显示了这本书的细节。
我可以成功显示搜索项目并在下一页显示。但是如何从类中使用此下载属性并将其链接到所选书籍。因此,当用户点击下载选项时,它应该导航到Web浏览器并应下载相应的文件。
我正在使用它 - 电子书api。请参阅此链接以获取有关api。API link here
的详细信息private async void search_btn_Click(object sender, RoutedEventArgs e)
{
try
{
using (HttpClient client = new HttpClient())
{
pgbar.Visibility = Visibility.Visible;
var uri = "http://it-ebooks-api.info/v1/search/{0}";
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(String.Format(uri, tb1.Text));
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
var listdata = JsonConvert.DeserializeObject<RootObject>(data.Result);
if (listdata != null && listdata.Books != null && listdata.Books.Count > 0)
lv1.ItemsSource = listdata.Books;
}
我需要使用其他API来下载所选书籍。
private async void download_btn_Click(object sender, RoutedEventArgs e)
{
using (HttpClient client = new HttpClient())
{
var uri = "http://it-ebooks-api.info/v1/book{}";
//Here I should implement some code to convert from json to simple download url
await Launcher.LaunchUriAsync(new Uri(uri));
}
[![在此输入图片说明] [4]] [4] 我应该在下一页显示标题和说明。所以我在新页面中绑定了listview控件。我还应该下载所选书籍的书
我得到了以下异常 [![在此处输入图像说明] [5]] [5]
答案 0 :(得分:0)
使用listview SelectedIndexChanged事件处理程序来了解所选项目,然后将每个项目转换为Book对象。由于您将列表“Books”分配给列表视图,因此您可以提取单个“Book”对象。请参阅代码中的注释。
private long bookid;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
dynamic bookDetails = e.Parameter;
bookid = bookDetails.ID;
}
private async void download_btn_Click(object sender, RoutedEventArgs e)
{
using (HttpClient client = new HttpClient())
{
var id = bookid;
var uri = "http://it-ebooks-api.info/v1/book/"+id;
//Here I should implement some code to convert from json to simple download url
await Launcher.LaunchUriAsync(new Uri(uri));
}
答案 1 :(得分:0)
我假设您将图书ID从搜索列表页面传递到图书详细信息页面
//Class for book details
public class BookDetails
{
private string Error {get; set;}
private string Time {get; set;}
private string ID {get; set;}
private string Title {get; set;}
private string SubTitle {get; set;}
private string Description {get; set;}
private string Author {get; set;}
private string ISBN {get; set;}
private string Year {get; set;}
private string Page {get; set;}
private string Publisher {get; set;}
private string Image {get; set;}
private string Download {get; set;}
}
private async void download_btn_Click(object sender, RoutedEventArgs e)
{
string downloadbookurl=string.empty;
//Download the book detail using API http://it-ebooks-api.info/v1/book/2279690981
string result=GetResult("http://it-ebooks-api.info/v1/book{book_isbn}")
//Deserialise the json data
BookDetails bookDetails= JsonConvert.DeserializeObject<BookDetails >(result);
if(bookDetails!=null)
downloadbookurl=bookDetails.Download ;
}
//Launch the webview
await Launcher.LaunchUriAsync(new Uri(downloadbookurl));
}
private async string GetResult(string url)
{
string response=string.empty;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(String.Format(uri, tb1.Text));
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
response=data.Result.ToString();
}
}
}