在APP中,用本地数据库好于频繁的联网去获取相关数据。我使用SQLite作为本地的数据库,比较轻巧。
英雄
首先先建立英雄的数据库,暂时我先只设置ID,name,loaclized_name这三种,我这是在窗体应用程序里写的,不要在意那些细节,其实我们只是要先创建这么一个.db文件
public Form1() { InitializeComponent(); databaseInit(); String url = "https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0001/?key=<key>&language=zh_cn"; WebClient web = new WebClient(); web.Encoding = System.Text.Encoding.UTF8; String html = web.DownloadString(url); load(html); } private void load(String json) { JObject jo = JObject.Parse(JObject.Parse(json)["result"].ToString()); JArray ja = JArray.Parse(jo["heroes"].ToString()); for (int i = 0; i < ja.Count; i++) { String name = ja[i]["name"].ToString(); String id = ja[i]["id"].ToString(); String local = ja[i]["localized_name"].ToString(); String sql = "INSERT INTO heroes(id,name,localized_name)VALUES(" + id + ",‘" + name + "‘,‘" + local + "‘)"; cmdInit(sql).ExecuteNonQuery(); } } //数据库初始化 public void databaseInit() { //数据库文件地址 String datasource = System.Windows.Forms.Application.StartupPath + "\\dota.db"; //连接数据 conString.DataSource = datasource; //可添加设置密码 constr.Password=""; sqlCon.ConnectionString = conString.ToString(); //创建数据库文件并连接 sqlCon.Open(); //创建表 try { String friendsql = "CREATE TABLE heroes (id integer PRIMARY KEY AUTOINCREMENT not null,name VARCHAR(64) not null," + " localized_name VARCHAR(64));"; cmdInit(friendsql).ExecuteNonQuery(); } catch (Exception) { } } //SQLiteCommand初始化配置 private SQLiteCommand cmdInit(String sql) { SQLiteCommand sqlCmd = new SQLiteCommand(); sqlCmd.Connection = sqlCon; sqlCmd.CommandText = sql; return sqlCmd; }
用软件打开后: