【发布时间】:2018-04-07 08:44:29
【问题描述】:
当他连接到互联网时,我正在尝试将一组 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();
}
}
【问题讨论】:
-
请提供完整的 url 以检查 url 是否发送响应?
-
而您希望有人具体做什么?猜你的问题?为你写代码,或者只是惊叹于这个问题缺乏细节。抱歉,这可能有点苛刻,但你必须从我们的角度来看这个