2017-05-28 49 views
0

我开始学习Xamarin开发应用为iOS,Android和Windows Phone。入门在Visual Studio中Xamarin远程URL一个JSON不工作

似乎一个简单的问题,但我不知道如何与Xamarin进行。

我有下面的代码行:

public async Task<List<MyData>> GetItemsAsync() 
{ 
     HttpClient client = new HttpClient(); 
     try { 
      string result = await client.GetStringAsync("https://jsonplaceholder.typicode.com/posts/1"); 
      if(!string.IsNullOrWhiteSpace(result)) { 
      } 
     } catch(Exception ex) { 
      return await Task.FromResult(new List<MyData>(0)); 
     } finally { 
      client.Dispose(); //of course, I can use `using` 
     } 

     return await Task.FromResult(new List<MyData>(0)); 
} 

当调试在Visual Studio Android模拟器(VisualStudio_android-23_x86_phone (Android 6.0 - API 23)),其中有string result开头的行被卡住,没有任何反应,不会引发任何异常。我无法在Visual Studio中使用F10进行检查。

另外,我无法使用WebClient因为System.Net不包括它。

编辑:如果我删除await然后我收到一般性例外:

enter image description here

哪里是我的错?

+0

你确定你的URI是可达的从设备/模拟器?你有没有等待查看请求是否最终超时? – Jason

+0

是的,我尝试过自己,并在'Browser'浏览器中显示。 –

+0

我看到的问题是使用HTTPS,如果我使用'http'将正常工作......如何解决'https'问题? –

回答

0

对于那些谁满足错误: Error: SecureChannelFailure (The authentication or decryption has failed.)

请使用特定的处理程序为Android:

using (HttpClient myClient = new HttpClient(new AndroidClientHandler())) { } 

或为iOS:

using(HttpClient myClient = new HttpClient(new CFNetworkHandler())) {} 

参考文献:

https://developer.xamarin.com/guides/cross-platform/macios/http-stack/ https://developer.xamarin.com/guides/android/application_fundamentals/http-stack/