2016-11-22 96 views
0

我正在关注如何从数据库站点接收数据的文档,在该数据库站点中有我需要的我自己的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

回答

0

API的文档明确指出/ raw/product /通话需要{Id}属性,以获取您希望获取的产品的ID。

要获得列表的产品,请使用/ raw/product/getpaged/ 这仍然需要一个参数,在这种情况下{pagesize},这意味着您仍然需要为每页所需的多少产品添加一个参数检索。

正确的通话将类似于“/生/产品/ getpaged/10”

你可以在你的浏览器验证,试图“GET”刚刚/生/产品/将导致404错误你在你的代码中看到。 为ID导致“未授权访问”错误,因为我没有你的密钥和秘密。

+0

Okok。我只使用phpmyadmin之前,我不认为这个数据库系统有一个类似的设置,所以我不知道我在哪里可以找到我需要的id。当我使用我的代码,并将httpadress更改为您建议的一个,而我仍然得到“解析值时遇到意外的字符:<。路径”,第0行,位置0.“错误。我尝试了不同的ID,但我不断得到同样的错误。 –

+0

我是一个小公司的实习生,这是他们存储他们的数据的系统,所以我很不确定它是如何工作的,而且他们也不太了解 –

+0

错误“”解析值时遇到意外的字符: <。路径“”仅仅来自http响应,它包含一个html错误页面,你正试图解析它,就好像它是一个json对象。请运行System.Diagnostics.Debug.WriteLine(resultString);再次显示您现在正在获取的整个错误消息。也请包括你现在打电话的网址 – irreal