【发布时间】:2019-02-20 19:00:39
【问题描述】:
我正在尝试从 WebAPI 应用程序中的 MySQL 数据库检索一组数据,并通过来自移动应用程序的 HTTP 请求访问它。因此,我创建了一个 WebApi、一个 RestClient 类和显示数据的类,这是我的代码。
网络 API
[Produces("application/json")]
[Route("api/Blog")]
public class BlogController : Controller
{
// GET: api/Blog
[HttpGet]
public IEnumerable<string> Get()
{
}
// GET: api/Blog/5
[HttpGet("{id}", Name = "GetBlogItems")]
public string Get(int id)
{
}
// POST: api/Blog
[HttpPost]
public void Post([FromBody] RetrieveDataClass value)
{
string sqlstring = "server=; port= ; user id =;Password=;Database=;";
MySqlConnection conn = new MySqlConnection(sqlstring);
try
{
conn.Open();
}
catch (MySqlException ex)
{
throw ex;
}
string Query = "INSERT INTO test.blogtable (id,Telephone,CreatedSaved,Topic,Summary,Category,Body1,Body2,Body3,Body4)values('" + value.TopicSaved1 + "','" + Value.Telephone + "','" + Value.Created/Saved + "','" + value.TopicSaved1 + "','" +value.SummarySaved1 +"','" +value.CategoriesSaved1 +"','" +value.Body1 +"','" +value.Body2 +"','" +value.Body3 +"','" +value.Body4 +"');";
MySqlCommand cmd = new MySqlCommand(Query, conn);
cmd.ExecuteReader();
conn.Close();
}
// PUT: api/Blog/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
因此,在我的数据库中,我有三行电话号码为 +233892929292,在过滤器之后我必须得到三行。而且我也会过滤到仅主题和摘要列。
RestClient 类
public class BlogRestClient<T>
{
private const string WebServiceUrl = "http://localhost:57645/api/Blog/";
public async Task<List<T>> GetAsync()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(WebServiceUrl);
var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
return taskModels;
}
public async Task<bool> PostAsync(T t)
{
var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(t);
HttpContent httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await httpClient.PostAsync(WebServiceUrl, httpContent);
return result.IsSuccessStatusCode;
}
public async Task<bool> PutAsync(int id, T t)
{
var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(t);
HttpContent httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await httpClient.PutAsync(WebServiceUrl + id, httpContent);
return result.IsSuccessStatusCode;
}
public async Task<bool> DeleteAsync(int id, T t)
{
var httpClient = new HttpClient();
var response = await httpClient.DeleteAsync(WebServiceUrl + id);
return response.IsSuccessStatusCode;
}
}
模型数据类
public class ModelDataClass
{
public string Telephone ;
public string Created/Saved ;
public string TopicSaved1 ;
public string SummarySaved1 ;
public string CategoriesSaved1 ;
public string Body1 ;
public string Body2 ;
public string Body3 ;
public string Body4 ;
public ModelDataClass()
{
}
}
ModelDataClass 中字符串的值设置在另一个类中以发布到 MySQL 数据库中。由于这不会导致有问题的问题,因此我没有包含代码。
检索数据类
public class RetrieveDataClass
{
public string Topic ;
public string Summary ;
public RetrieveDataClass()
{
GetDataEvent();
AddBlog();
}
public void GetDataEvent()
{
BlogRestClient<ModelDataClass> restClient = new
BlogRestClient<ModelDataClass>();
await restClient.GetAsync();
}
public ObservableCollection<ModelDataClass> BlogItems = new
ObservableCollection<ModelDataClass>();
public void AddBlog()
{
BlogListView.ItemsSource = BlogItems;
}
}
问题1 如何将数据从 Mysql 检索到通过 REST 客户端类访问的 WebAPI(它适用于移动设备,所以我必须使用 Http 请求)?
问题2 我想为通过 MySQL 数据库检索的每一行创建一个 listView。标题为主题列的数据,副标题为摘要列的数据。
【问题讨论】:
-
您需要将 MySQL 连接器添加到您的服务器,然后使用
System.Data和MySQL.Data库来检索数据。见-dev.mysql.com/doc/connector-net/en/…。 -
@Kami 当然我已经添加了 MysqlConnector 并连接到服务器,那么你如何假设我能够发布数据(如果这就是你的意思)。而且即使我没有连接到服务器,你的回答也不能解决任何问题,谢谢。
-
你有两个问题,1,如何获取数据。 MySQL 文档很好地概述了如何连接到 MySQL 并执行查询以检索数据。当服务器响应 GET 请求时使用 REST,它不会 POST - 您需要使用相关数据响应 GET 请求。
-
@Kami p 你能用我仍然不明白的代码写出来吗,谢谢。
标签: c# mysql asp.net-web-api xamarin.forms rest-client