我正在关注如何从数据库站点接收数据的文档,在该数据库站点中有我需要的我自己的API密钥。不过,我无法访问JSON数据。从数据库收集JSON并收到它在Xamarin Forms上收到问题
这些是我关注的文件。
https://cbis-rest-api.citybreak.com/v1/swagger/ui/index
https://visit.github.io/api-doc/
这是我的代码目前的样子:
static public class myData
{
static string apiKey = "myApiKeyNumber";
static string apiApp = "application/json";
static public async Task<JObject> testing()
{
var httpClientRequest = new HttpClient();
httpClientRequest.DefaultRequestHeaders.Add("ApiKey", apiKey);
httpClientRequest.DefaultRequestHeaders.Add("Accept", apiApp);
var result = await httpClientRequest.GetAsync("https://cbis-rest-api.citybreak.com/v1/api/raw/product/");
var resultString = await result.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(resultString);
var jsonResult = JObject.Parse(resultString);
System.Diagnostics.Debug.WriteLine(jsonResult);
}
}
当我想用它工作:
async void loadData()
{
var getInfo = await phpApi.testing();
if (getInfo == null)
{
System.Diagnostics.Debug.WriteLine("no data");
}
else {
System.Diagnostics.Debug.WriteLine("data");
}
}
我认为,我输入JObject任务的httpadress必须是wron g因为当我运行这段代码时,我得到了崩溃:“解析值时遇到意外的字符:<。路径“,第0行,位置0.”。
当我运行“resultString”System.Diagnostics.Debug.WriteLine(resultString);在日志中我得到这个:
<!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml/DTD/xhtm1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
<style type="text/css">`
这意味着,HTTP(https://cbis-rest-api.citybreak.com/v1/api/raw/product/)不具备JSON。 当我看到文档时,我认为我遵循正确的说明,但是我有点困惑。
任何帮助,指导将非常感激。我不习惯使用数据库,所以我非常感谢帮助!
更新:
static public class myData
{
static string apiKey = "myApiKeyNumber";
static string apiApp = "application/json";
static public async Task<JObject> testing()
{
var httpClientRequest = new HttpClient();
httpClientRequest.DefaultRequestHeaders.Add("ApiKey", apiKey);
httpClientRequest.DefaultRequestHeaders.Add("Accept", apiApp);
var result = await httpClientRequest.GetAsync("https://cbis-rest-api.citybreak.com/v1/api/raw/product/getpaged/1");
var resultString = await result.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(resultString);
var jsonResult = JObject.Parse(resultString);
return jsonResult;
}
}
有了这个代码,我得到的错误“错误从JsonReader,路径读取JObject”,行0,位置0" ,而我什么也没有在日志中来自这意味着resultString这里没有JSON?我尝试了许多不同的ID,但结果相同我不知道我在哪里找到数据的ID
Okok。我只使用phpmyadmin之前,我不认为这个数据库系统有一个类似的设置,所以我不知道我在哪里可以找到我需要的id。当我使用我的代码,并将httpadress更改为您建议的一个,而我仍然得到“解析值时遇到意外的字符:<。路径”,第0行,位置0.“错误。我尝试了不同的ID,但我不断得到同样的错误。 –
我是一个小公司的实习生,这是他们存储他们的数据的系统,所以我很不确定它是如何工作的,而且他们也不太了解 –
错误“”解析值时遇到意外的字符: <。路径“”仅仅来自http响应,它包含一个html错误页面,你正试图解析它,就好像它是一个json对象。请运行System.Diagnostics.Debug.WriteLine(resultString);再次显示您现在正在获取的整个错误消息。也请包括你现在打电话的网址 – irreal