0

php我在代码中没有收到错误,但它仍然没有在应用程序中显示任何输出,我正在尝试获取数据表单MySql作为Json响应。该应用程序仅显示加载栏,但未加载任何数据,请帮助我。

{"Hotel":[{"HotelID":"35"},{"HotelID":"85"},{"HotelID":"90"},{"HotelID":"92"},{"HotelID":"94"},{"HotelID":"106"},{"HotelID":"111"},{"HotelID":"115"},{"HotelID":"175"},{"HotelID":"184"},{"HotelID":"185"},{"HotelID":"186"},{"HotelID":"187"},{"HotelID":"188"},{"HotelID":"189"},{"HotelID":"190"},{"HotelID":"191"},{"HotelID":"192"},{"HotelID":"193"},{"HotelID":"194"},{"HotelID":"195"},{"HotelID":"198"},{"HotelID":"201"},{"HotelID":"203"},{"HotelID":"205"},{"HotelID":"206"},{"HotelID":"208"},{"HotelID":"211"},{"HotelID":"212"},{"HotelID":"213"},{"HotelID":"214"}]}

C#

 namespace jsontest
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        Loaded += MainPage_Loaded;
    }
    WebClient client;
    ProgressIndicator prog;
    private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        string url = "my server url";
        Uri uri = new Uri(url);
        client = new WebClient();
        client.AllowReadStreamBuffering = true;

        client.DownloadStringCompleted += client_DownloadStringCompleted;
        client.DownloadProgressChanged += client_DownloadProgressChanged;

        prog = new ProgressIndicator();
        prog.IsVisible = true;
        prog.IsIndeterminate = true;
        prog.Text = "Loading....";
        SystemTray.SetProgressIndicator(this, prog);
    }
    private void client_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e) 
    {
        if (e.Cancelled == false & e.Error == null)
        {
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
            ObservableCollection<Hotel> list = new ObservableCollection<Hotel>();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Hotel>));
            list = (ObservableCollection<Hotel>)serializer.ReadObject(ms);

            List<Hotel> myHotel = new List<Hotel>();

            foreach (Hotel cm in list)
            {
                string strHotelID = cm.HotelID.ToString();

                myHotel.Add(new Hotel(strHotelID));
            }
            this.HotelList.ItemsSource = myHotel;

            prog.IsVisible = false;
        }

    }
    private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {

    }

}
[DataContract]
public class Hotel
{
    [DataMember]
    public string HotelID { get; set; }

    public Hotel(string strHotelID)
    {
        this.HotelID = strHotelID;
    }
   }

}
4

1 回答 1

0

您错过了对DownloadStringAsync的调用

事实上,您的代码根本不会下载任何内容。

至少尝试这样的事情

string url = "my server url";
Uri uri = new Uri(url);
client = new WebClient();
client.DownloadStringAsync(uri );
client.AllowReadStreamBuffering = true;
于 2013-07-23T11:40:08.977 回答