2017-05-01 5 views
0

まず、私は何度も何度も読んでいると言いたいですが、それは助けになりません。Xamarin HttpClient jsonをフェッチして使用する方法

私は尋ねたときにJSONを返すphpwebserviceを持っている:

<?php 

include("../include/class.pdogsb.inc.php"); 


$pdo = PdoGsb::getPdoGsb(); 


$tabCabinets = $pdo->getLesCabinets(); 



    header('Content-type: application/json'); 
    echo json_encode(array('cabinets'=>$tabCabinets)); 

?> 

それがこのタイプIはI私が使用するメソッドを作成している私のxamarin PCLプロジェクトで

cabinets": [ 

    { 
     "0": "1", 
     "1": "2,2891506", 
     "2": "48,8618687", 
     "3": "75016", 
     "4": "Paris", 
     "5": "1 Avenue Gustave V de Suède", 
     "id": "1", 
     "longitudeGPS": "2,2891506", 
     "latitudeGPS": "48,8618687", 
     "cp": "75016", 
     "ville": "Paris", 
     "rue": "1 Avenue Gustave V de Suède" 
    }, 

の応答を与えますそのjsonデータを取得するために私のビューをロードします。

using Newtonsoft.Json; 
    using suivAAndroid.Models; 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Linq; 
    using System.Net.Http; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace suivAAndroid.ViewModel 
    { 
     public partial class CreerVisiteViewModel : INotifyPropertyChanged 
     { 
      #region propriétés 
      private Cabinet unCabinet; 
      private Medecin unMedecin; 

      public event PropertyChangedEventHandler PropertyChanged; 

      #endregion 

      #region methods 
      public CreerVisiteViewModel() 
      { 
       loadcabinets(); 
      } 
      private async Task loadcabinets() 
      { 
       List<Cabinet> listeCabinets = null; 
       HttpClient clientCabinets = new HttpClient(); 
       string url = "http://10.0.0.5/ppe3JoJuAd/gsbAppliFraisV2/webservices/w_cabinet.php"; 
//does not read the code after this line 
       var response = await clientCabinets.GetAsync(new Uri(url)); 
       string jsonString = await response.Content.ReadAsStringAsync(); 
       listeCabinets = JsonConvert.DeserializeObject<List<Cabinet>>(jsonString); 
      } 

      #endregion 
     } 
    } 

私の問題は、実行中のアプリのデバッグ中に、コンパイラはstring url = "10.0.0.5/ppe3JoJuAd/gsbAppliFraisV2/webservices/w_cabinet.php";行の後に行かない、です。その代わりに、loadcabinets();に戻ります。これは、クライアントがjsonデータを決して取得しないことを意味します。

私は何ができますか?

編集:最近の答えと更新されたコード

私はここで

public class RelayCommandAsync : ICommand 
    { 
     private Func<object, Task> _action; 
     private Task _task; 

     public RelayCommandAsync(Func<object, Task> action) 
     { 
      _action = action; 
     } 

     public bool CanExecute(object parameter) 
     { 
      return _task == null || _task.IsCompleted; 
     } 

     public event EventHandler CanExecuteChanged; 

     public async void Execute(object parameter) 
     { 
      _task = _action(parameter); 
      OnCanExecuteChanged(); 
      //await _task; 
      //OnCanExecuteChanged(); 
     } 

     private void OnCanExecuteChanged() 
     { 
      var handler = this.CanExecuteChanged; 
      if (handler != null) 
       handler(this, EventArgs.Empty); 
     } 
    } 

iCommandのインターフェイスを実装どこでBaseViewModelを作成したが、結果はそれはまだありません私の以前のViewModelに

#region methods 
     public CreerVisiteViewModel() 
     { 


     } 
     #endregion 
     #region Methods 
     public async Task<List<Cabinet>> loadcabinetsAsync() 
     { 
      List<Cabinet> listeCabinets = null; 
      HttpClient clientCabinets = new HttpClient(); 
      try 
      { 
       string url = "http://10.0.0.5/ppe3JoJuAd/gsbAppliFraisV2/webservices/w_cabinet.php"; 
       var uri = new Uri(string.Format(url, string.Empty)); 
       var response = await clientCabinets.GetAsync(uri); 
       string jsonString = await response.Content.ReadAsStringAsync(); 
       listeCabinets = JsonConvert.DeserializeObject<List<Cabinet>>(jsonString); 

      } 
      catch (Exception e) 
      { 

      } 
      return listeCabinets; 
     } 
     #endregion 


     #region ICommand 
     public ICommand ExecuteLoadCabinetAsync { get { return new RelayCommandAsync(x => loadcabinetsAsync()); } } 
     #endregion 

ですコンパイラの問題を解決しないで、すべてのメソッドのコードを読んでいない、もっと何ができますか?

答えて

1

いくつかの問題がここにあります

  1. あなたはそれを待たずに非同期メソッドを呼び出しています。あなたがあなたのURLが有効ではありません前後に
  2. あなたはコンテキストを切り替えている意味、ConfigureAwait(false)を使用せずに、右の別の後、いくつかの非同期メソッドを呼び出しているコンストラクタ
  3. で非同期メソッドを呼び出している
  4. スキームは、あなたがMVVMパターンを使用したい場合は、おそらくそこ

例外を取得しているので、あなたはICommandであなたのコードをラップだろう、とObservableCollectionを移入するために、そのコマンドを実行します。このコマンドの実行は、使用しているプラ​​ットフォームに応じてViewController/Activity/PageのLifecycleイベントで発生します。

+0

1と2はどのように解決できますか?私は3を認識していませんでした。実際に、私は文字列の前にhttpを置くことで解決した無効なURI例外がスローされました。 –

+0

コンストラクタを非同期にすることはできませんので、Task: 'Task.Run(async()=> await loadcabinets());'で 'loadcabinets();'コールをラップしてください。 –

+0

推奨されるように、 'ICommand'を使ってそれを実行してください。 MvvmCross、ReactiveUI、MvvmLightなどのフレームワークは、すでに使用できるように実装されています。 – Cheesebaron

関連する問題