2011-12-17 5 views
0

私はWindows Phone7アプリを開発するために新しいです、これは私がjsonデータをポストしている小さなWP7アプリケーションを開発しました。成功のメッセージと1つのid。今私は成功のメッセージとIDを取得したい。私は戻ってデータを得ることができる方法を知って誰も私をここから移動してください助けてください。ここで私はjsonを投稿したコードを投稿します。ウィンドウphone7で私のjsonデータを投稿した後、返信json成功メッセージを取得する方法

  private void SendOrder_Click(object sender, EventArgs e) 
    { 
     Double grossTotal = 0.0; 
     List<MenuItem> mitems = new List<MenuItem>(); 

     foreach (var item in RestaurantApp.ViewModel.Generic.Orders) 
     { 
      grossTotal += Convert.ToDouble(item.OrderTotal.TrimStart(new char[] { '$' })); 

     } 

     DateTime MyDateTime = ((DateTime)DateToDialIn.Value).Date.Add(((DateTime)TimeToDialIn.Value).TimeOfDay); 
     ViewModel.RootObject root = new ViewModel.RootObject() 
     { 
      order = new ViewModel.Orders() 
      { 
       LocationId = Convert.ToInt32(RestaurantApp.ViewModel.Generic.LocationPoco.LocationId), 
       DeviceIdentifier = Convert.ToBase64String((byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId")), 
       OrderContactName = txtName.Text, 
       OrderContactPhone = txtPhone.Text, 
       OrderContactEmail = txtEmail.Text, 
       ShipMethod = RestaurantApp.ViewModel.Generic.ShipMethod, 
       PickupDate = ((DateTime)DateToDialIn.Value).Date.Add(((DateTime)TimeToDialIn.Value).TimeOfDay).ToString(), 
       Amount = grossTotal.ToString(), 
       items = returnlist(mitems) 
      }, 

     }; 

     string json = null; 
     WebClient client = new WebClient(); 
     client.Headers["Content-Type"] = "application/json"; 

     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ViewModel.RootObject)); 
     using (MemoryStream stream = new MemoryStream()) 
     { 
      serializer.WriteObject(stream, root); 
      //stream.Flush(); 
      json = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length); 
     } 
     client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted); 
     client.UploadStringAsync(new Uri("http://api.mybusinessapp.com/restaurant/PlaceOrder"), "POST", json); 
     string responce = client.ResponseHeaders.ToString(); 
     } 

     void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) 
    { 

     RestaurantApp.ViewModel.Generic.Orders = null; 
     RestaurantApp.ViewModel.Generic.ShipMethod = null; 
     NavigationService.Navigate(new Uri("/Menu.xaml?LocationGUID=" + RestaurantApp.ViewModel.Generic.LocationPoco.LocationGuid, UriKind.Relative)); 
    } 

答えて

0

あなたはこのようにヘルパーメソッドを準備することができます:私は解決し、リプレイを与えてくれてありがとう...

WebClient client = new WebClient(); 
    client.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) 
     { 
      Stream stream = e.Result; 
      StreamReader reader = new StreamReader(stream); 
      yourResult = reader.ReadToEnd().JsonTo<YourResult>(); 

     }; 

    client.OpenReadAsync(new Uri("http://your_api")); 
+0

こんにちは:

public static T JsonTo<T>(this string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T jsonObject; try { jsonObject = (T)ser.ReadObject(ms); } catch (System.Runtime.Serialization.SerializationException err) { ms.Close(); return default(T); } ms.Close(); return jsonObject; } 

はあなたの結果のJSONがYourResultインスタンスを表すと仮定します私の問題は、ヘルパーmethod.iを使用してe.Resultを使用して返信メッセージを取得します。 – tiru

関連する問題