如何从网站获取json数据到我的Xamarin android应用程序

时间:2018-04-07 08:44:29

标签: c# android json xamarin

当我连接到互联网时,我试图从用户手机上的网站上获取一个Json数据数组到sqlite数据库。我正在使用Newtonsoft.json nuget包。我测试时没有任何反应。 这是我试过的

我在MainActivity中有一个方法,它应该从网站读取数据,然后使用另一种方法将数据添加到本地sqlite数据库,如下所示。

    public void GetAllPharmacies()
    {
        try
        {

            var client = new HttpClient();
            var uri = "http://no-nonsense-caliber.000webhostapp.com/ajax/GetAllPharmacies.php";
            var result = client.GetStringAsync(uri);
            var pharms = JsonConvert.DeserializeObject<List<Model1>>(result.ToString());
            foreach (Model1 pharm in pharms)
            {
                AddNewPharmacy(pharm.name, pharm.address, pharm.phone, pharm.email, pharm.dp, pharm.gmap, pharm.city);
            }
        }
        catch (Exception ex)
        {
            Toast.MakeText(this, ex.Message, ToastLength.Long);
        }

    }
}

   public class Model1
   {
    public int id { get; set; }
    public string name { get; set; }
    public string address { get; set; }
    public string phone { get; set; }
    public string email { get; set; }
    public string dp { get; set; }
    public string gmap { get; set; }
    public string city { get; set; }

   }

private void AddNewPharmacy(string name, string address, string phone, string email, string dp, string gmap, string city)
    {
        var databasePath = Path.Combine(FilesDir.AbsolutePath, "phidia.db");
        var connectionString = string.Format("Data Source={0}", databasePath);
        SqliteConnection conn = new SqliteConnection(connectionString);
        string insertQuery = @"INSERT INTO [Pharmacies] (name,address, phone, email, dp, gmap, city)VALUES (@name,@address, @phone, @email, @dp, @gmap, @city);";
        using (var cmd = new SqliteCommand(insertQuery, conn))
        {
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@address", address);
            cmd.Parameters.AddWithValue("@phone", phone);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@dp", dp);
            cmd.Parameters.AddWithValue("@gmap", gmap);
            cmd.Parameters.AddWithValue("@city", city);
            cmd.ExecuteNonQuery();
        }
    }

2 个答案:

答案 0 :(得分:0)

您需要等待GetStringAsync获取字符串。目前你正在尝试反序列化Task.ToString而不是从网站返回的内容。使用async void并不好,所以我让函数返回一个布尔值。

public async Task<bool> GetAllPharmacies()
{
    try
    {

        var client = new HttpClient();
        var uri = "http://no-nonsense-caliber.000webhostapp.com/ajax/GetAllPharmacies.php";
        string result = await client.GetStringAsync(uri);
        var pharms = JsonConvert.DeserializeObject<List<Model1>>(result);
        foreach (Model1 pharm in pharms)
        {
            AddNewPharmacy(pharm.name, pharm.address, pharm.phone, pharm.email, pharm.dp, pharm.gmap, pharm.city);
        }
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.Message, ToastLength.Long);
    }
    return true;
}

答案 1 :(得分:0)

请关注Microsoft的这篇文章:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

该网站的一个例子:

static async Task<Product> GetProductAsync(string path)
        {

            Product product = null;
            HttpResponseMessage response = await client.GetAsync(path);
            if (response.IsSuccessStatusCode)
            {
                product = await response.Content.ReadAsAsync<Product>();
            }
            return product;
        }

您还可以将响应转换为变量并使用Newtonsoft Json对其进行反序列化。