|
Como fazer um GET num webservice feito em C# (retorno JSON)
Este exemplo retornar um List<string> apenas do nome do produto.
HTTP - webservice - http://teste.market.com.br/api/produto
[
{"Id":1,"Nome":"Hariel","Valor":9.2},
{"Id":97,"Nome":"Gelo Seco","Valor":1.99},
{"Id":1002,"Nome":"Hanna2","Valor":1.1}
]
Sistema em Xamarin
using System;
using System.Net.Http;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
namespace Funcoes
{
public class Comunicacao
{
public async Task<List<string>> GetAsync(string param)
{
string url = string.Format ("http://teste.market.com.br/api/produto/{0}", param);
var client = new HttpClient ();
client.DefaultRequestHeaders.Add ("User-Agent", "Other");
var response = await client.GetAsync (url);
var content = await response.Content.ReadAsStringAsync ();
var json = JArray.Parse (content);
var lista = new List<string> ();
foreach (var item in json) {
var repository = item.Value<string> ("Nome");
lista.Add (repository);
}
return lista;
}
}
}
|