2017-05-28 33 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/