如何显示存储在变量中的字符串,作为对来自服务器的请求的响应

时间:2012-03-25 20:56:12

标签: c# windows-phone-7

以下链接是请求:

http://www.discountbox.in/OrkutJSONServlet?locality=Amravati®ion=马哈拉施特拉&安培; maxOffers = 12&安培; offerRequestType = PromotedOffersRequest&安培;类别=所有

以下是主页的代码,我试图显示存储在名为'feed'的变量中的字符串:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneApp20
{
public class Transaction
{
    public string id, offer, image;

    public String ID { get; set; }
    public String Offer { get; set; }
    public String Image { get; set; }

    public Transaction(String id, String offer, String image)
    {
        this.id = id;
        this.offer = offer;
        this.image = image;
    }
}


public partial class MainPage : PhoneApplicationPage
{
    String soffer, sid, simage;
    String Offerid = "20";
    int i1, j1, k1;
    // Constructor
    Transaction t;

    public static String message = "Hello";

    public MainPage()
    {
        InitializeComponent();
        this.SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
        HttpWebRequest request = HttpWebRequest.CreateHttp("http://www.discountbox.in/OrkutJSONServlet?locality=Amravati®ion=Maharashtra&maxOffers=12&offerRequestType=PromotedOffersRequest&category=All");
        request.BeginGetResponse(new AsyncCallback(HandleResponse), request);



    }
    public void HandleResponse(IAsyncResult result)
    {

        HttpWebRequest request = result.AsyncState as HttpWebRequest;



        if (request != null)
        {

            using (WebResponse response = request.EndGetResponse(result))
            {

                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {

                    string feed = reader.ReadToEnd();
                      Parse(feed);

                }
            }
        }
    }
    private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {

    }

    private void init()
    {
        TextBox tx = new TextBox();


    }
    private void Parse(string feed)
    {

        string data1 = feed;
        string[] tokens;
        string[] id, image, offer;
        string str;
        char[] sep = { ',' };

        int x, y;


        x = feed.IndexOf("[");
        y = feed.IndexOf("]");
        str = feed.Substring(x + 1, y - x - 1);

        tokens = str.Split(sep);
        id = new string[tokens.Length];
        offer = new string[tokens.Length];
        image = new string[tokens.Length];
        string[] tt = new String[tokens.Length];
        int j = 0;
        for (int i = 0; i < tokens.Length; i++)
        {
            tt[j] = tokens[i].Substring(1, tokens[i].Length - 2);
            j++;

        }

        i1 = 0; j1 = 1; k1 = 2;
        int m = 0;
        int maxcount = tt.Length;
        for (int i = 0; i < maxcount / 3; i++)
        {
            if (i >= tt.Length)
                break;
            id[m] = tt[i1];
            id[m] = id[m].Replace("\"", "");
            image[m] = tt[j1];
            image[m] = image[m].Replace("\"", "");
            image[m] = image[m].Replace("\\/\\/", "\\\\");
            image[m] = image[m].Replace("\\/", "\\\\");
            offer[m] = tt[k1];
            offer[m] = offer[m].Replace("\"", "");
            i1 += 3;
            j1 += 3;
            k1 += 3;
            m++;
        }
        soffer = offer[0];
        sid = id[0];
        simage = image[0];

        t = new Transaction(sid, soffer, simage);

        //for (i1 = 0; i1 < maxcount / 3; i1++)
        //{
        //    Console.WriteLine("Id = {0}  ", id[i1]);
        //    Console.WriteLine("Offer = {0}  ", offer[i1]);
        //    Console.WriteLine("Image = {0}  ", image[i1]);
        //}
        //Console.ReadLine();



    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {

    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

}
}

现在,我已经尝试了每个可用的控件来显示字符串,但它不会在模拟器上显示任何内容。

1 个答案:

答案 0 :(得分:0)

要在屏幕上显示字符串,最简单的方法是在页面上显示TextBlock并更新它的Text属性。