2011-12-17 2 views
0

나는 Windowsphone7 응용 프로그램을 개발하는 데 새로운데, 나는이 json 데이터를 게시 한 작은 WP7 응용 프로그램을 개발했습니다. 성공 메시지와 하나의 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 인스턴스를 나타냅니다 가정 helper method.i를 사용하여 내 문제가 e.Result를 사용하여 반환 메시지를받습니다. 재생에 감사드립니다 ... – tiru

관련 문제