我怎样才能解开Compact Framework的客户端上的自定义类型的返回泛型列表? [英] How can I unpack a returned generic list of a custom type on the Compact Framework client?

查看:116
本文介绍了我怎样才能解开Compact Framework的客户端上的自定义类型的返回泛型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从本地到它的MS Access表,并存储值到一个通用的列表,并传递回读的Web API服务器上的REST方法:

 公开名单<&Sitemap产生GT; GetSiteMappings(字符串userid,字符串PWD)
{
    清单<&Sitemap产生GT; siteMappings =新的List<&Sitemap产生GT;();
    串connStr =
        的String.format(
            @供应商= Microsoft.ACE.OLEDB.12.0;用户ID = {0};密码= {1};数据
来源= C:\\ CDBWin \\ DATA \\ CDBSetup.MDB;喷射OLEDB:系统数据库= C:\\ CDBWin \\ DATA \\ sscs.mdw,用户ID
PWD);
    使用(VAR康恩=新的OleDbConnection(connStr))
    {
        使用(OleDbCommand的CMD = conn.CreateCommand())
        {
            cmd.CommandText =SELECT site_no,location_num从site_no t_sites秩序;
            cmd.CommandType = CommandType.Text;
            conn.Open();
            使用(OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
            {
                而(oleDbD8aReader = NULL&放大器;!&安培; oleDbD8aReader.Read())
                {
                    INT siteNum = oleDbD8aReader [SiteNumber]是的DBNull
                        ? 0
                        :Convert.ToInt16(oleDbD8aReader [SiteNumber]);
                    字符串locationNum = oleDbD8aReader [location_num]是的DBNull
                        ?的String.Empty
                        :oleDbD8aReader [location_num]的ToString();
                    siteMappings.Add(新Sitemap产生{SiteNumber = siteNum,LocationNumber =
locationNum});
                }
            }
        }
    }
    返回siteMappings;
}公共类第三方Sitemap
{
    公众诠释SiteNumber {搞定;组; }
    公共字符串LocationNumber {搞定;组; }
    公共字符串网站名称{搞定;组; }
}

我把它从客户端(Windows CE的/ Compact Framework的),像这样:

 公开的ArrayList FetchSiteMappings(字符串URL)
{
    HttpWebRequest的httpwreq = SendHTT prequestNoCredentials(URL,HttpMethods.GET,的String.Empty,应用程序/ XML);
    //从http://stackoverflow.com/questions/12350670/how-to-extract-zipped-file-received-from-httpwebresponse理念
    变种响应= httpwreq.GetResponse();
    变种responseStream = response.GetResponseStream();
    responseStream。 < =我卡在这里
}

...但我不知道如何处理响应流做 - 我如何把什么传递从一个普通的列表返回一个ArrayList

这里的 SendHTT prequestNoCredentials()的方法,如果任何人的兴趣(或者即使他们没有):

 公共静态的HttpWebRequest SendHTT prequestNoCredentials(字符串URI,HttpMethods方法,字符串数据,字符串的contentType)
{
    WebRequest的请求= NULL;
    尝试
    {
        请求= WebRequest.Create(URI);
        request.Method = Enum.ToObject(typeof运算(HttpMethods),法)的ToString();
        request.ContentType的contentType =;
        ((HttpWebRequest的)要求)。接受的contentType =;
        ((HttpWebRequest的)要求).KeepAlive = FALSE;
        ((HttpWebRequest的)要求).ProtocolVersion = HttpVersion.Version10;        如果(方法= HttpMethods.GET和放大器;!&安培;!方法= HttpMethods.DELETE)
        {
            字节[] = arrData Encoding.UTF8.GetBytes(数据);
            request.ContentLength = arrData.Length;
            使用(流OS = request.GetRequestStream())
            {
                oS.Write(arrData,0,arrData.Length);
            }
        }
        其他
        {
            request.ContentLength = 0;
        }
    }
    赶上(异常前)
    {
        字符串msgInnerExAndStackTrace =的String.Format(
                {0};内蒙古例如:{1};堆栈跟踪:{2},ex.Message,ex.InnerException,
ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(的String.Format(从
FileXferREST.SendHTT prequestNoCredentials(){0},msgInnerExAndStackTrace));
    }
    返回的请求为HttpWebRequest的;
}

更新

我已经改变了SendHTT prequestNoCredentials()方法返回,而不是一个一个的HttpWebRequest HttpWebResponse(见<一href=\"http://stackoverflow.com/questions/27804430/is-there-something-missing-from-this-borrowed-adapted-$c$c-or-am-i-not-seeing\">Is有什么从此却下落不明(借用/改编)code,还是我没有看到其中获取被炒鱿鱼?在更新那里(最小)的详细信息),所以FetchSiteMappings()方法也改变;现在是:

  HttpWebResponse httpwresp = SendHTT prequestNoCredentials(URL,HttpMethods.GET,的String.Empty,应用程序/ XML);
httpwresp。 &LT;如今=什么?


解决方案

看来,这应该工作:

 公开的ArrayList FetchSiteQuery(字符串URL)
{
    清单&LT;串GT; LS =新的List&LT;串GT;();
    字节[] buf中=新的字节[8192];
    字符串tempString = NULL;
    诠释计数= 0;
    //从http://stackoverflow.com/questions/12350670/how-to-extract-zipped-file-理念
接收来自-httpwebresponse
    HttpWebResponse httpwresp = SendHTT prequestNoCredentials(URL,HttpMethods.GET,
的String.Empty,应用程序/ XML);
    变种responseStream = httpwresp.GetResponseStream();
    //改编自http://www.csharp-station.com/HowTo/HttpWebFetch.aspx以下
    做
    {
        //填充数据的缓冲区
        数= responseStream.Read(BUF,0,buf.Length);        //确保我们读一些数据
        如果(计数!= 0)
        {
            //从字节ASCII文本翻译
            tempString = Encoding.ASCII.GetString(BUF,0,计数);            //继续生成字符串
            ls.Add(tempString);
        }
    }
    而(计数大于0); //更多的数据看?
    ArrayList的myArrayList =新的ArrayList();
    myArrayList.AddRange(LS);
    返回myArrayList;
}

更新

是的,它的工作;本次测试code:

 私人无效button56_Click(对象发件人,EventArgs的发送)
{
    ArrayList的人= FetchSiteQuery(HTTP://本地主机:21608 / API / sitequery / GETALL / dbill / PPU的/ 3);
    的foreach(一个String的人)
    {
        MessageBox.Show(多个);
    }
}

...给我这样的:

...还有几个后页面(消息框实例)。

I have a REST method on the Web API server that reads from a local-to-it MS Access table, and stores the values into a generic list, and passes that back:

public List<SiteMapping> GetSiteMappings(String userId, String pwd)
{
    List<SiteMapping> siteMappings = new List<SiteMapping>();
    string connStr =
        string.Format(
            @"Provider=Microsoft.ACE.OLEDB.12.0;User ID={0};Password={1};Data 
Source=C:\CDBWin\DATA\CDBSetup.MDB;Jet OLEDB:System database=C:\CDBWin\Data\sscs.mdw", userId, 
pwd); 
    using (var conn = new OleDbConnection(connStr))
    {
        using (OleDbCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = "SELECT site_no, location_num FROM t_sites order by site_no";
            cmd.CommandType = CommandType.Text;
            conn.Open();
            using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
            {
                while (oleDbD8aReader != null && oleDbD8aReader.Read())
                {
                    int siteNum = oleDbD8aReader["SiteNumber"] is DBNull
                        ? 0
                        : Convert.ToInt16(oleDbD8aReader["SiteNumber"]);
                    string locationNum = oleDbD8aReader["location_num"] is DBNull
                        ? string.Empty
                        : oleDbD8aReader["location_num"].ToString();
                    siteMappings.Add(new SiteMapping { SiteNumber = siteNum, LocationNumber = 
locationNum });
                }
            }
        }
    }
    return siteMappings;
}

public class SiteMapping
{
    public int SiteNumber { get; set; }
    public string LocationNumber { get; set; }
    public string SiteName { get; set; }
}

I call it from the client (Windows CE / Compact Framework) like so:

public ArrayList FetchSiteMappings(string url)
{
    HttpWebRequest httpwreq = SendHTTPRequestNoCredentials(url, HttpMethods.GET, String.Empty, "application/xml");
    // Idea from http://stackoverflow.com/questions/12350670/how-to-extract-zipped-file-received-from-httpwebresponse
    var response = httpwreq.GetResponse();
    var responseStream = response.GetResponseStream();
    responseStream. <= I'm stuck here
}

...but I don't know what to do with the response stream -- How do I convert what is passed back from a generic list to an ArrayList?

Here's the SendHTTPRequestNoCredentials() method, in case anybody's interested (or even if they're not):

public static HttpWebRequest SendHTTPRequestNoCredentials(string uri, HttpMethods method, string data, string contentType)
{
    WebRequest request = null;
    try
    {
        request = WebRequest.Create(uri);
        request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();
        request.ContentType = contentType;
        ((HttpWebRequest)request).Accept = contentType;
        ((HttpWebRequest)request).KeepAlive = false;
        ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;

        if (method != HttpMethods.GET && method != HttpMethods.DELETE)
        {
            byte[] arrData = Encoding.UTF8.GetBytes(data);
            request.ContentLength = arrData.Length;
            using (Stream oS = request.GetRequestStream())
            {
                oS.Write(arrData, 0, arrData.Length);
            }
        }
        else
        {
            request.ContentLength = 0;
        }
    }
    catch (Exception ex)
    {
        String msgInnerExAndStackTrace = String.Format(
                "{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, 
ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(String.Format("From 
FileXferREST.SendHTTPRequestNoCredentials(): {0}", msgInnerExAndStackTrace));
    }
    return request as HttpWebRequest;
}

UPDATE

I've changed the SendHTTPRequestNoCredentials() method to return a HttpWebResponse instead of a HttpWebRequest (see Is there something missing from this (borrowed/adapted) code, or am I not seeing where a Get gets fired? for the (minimal) details in the Update there), so the FetchSiteMappings() method has also changed; it is now:

HttpWebResponse httpwresp = SendHTTPRequestNoCredentials(url, HttpMethods.GET, String.Empty, "application/xml");
httpwresp. <= what now???

解决方案

It seems that this should work:

public ArrayList FetchSiteQuery(string url)
{
    List<String> ls = new List<string>();
    byte[] buf = new byte[8192];
    string tempString = null;
    int count = 0;
    // Idea from http://stackoverflow.com/questions/12350670/how-to-extract-zipped-file-
received-from-httpwebresponse
    HttpWebResponse httpwresp = SendHTTPRequestNoCredentials(url, HttpMethods.GET, 
String.Empty, "application/xml");
    var responseStream = httpwresp.GetResponseStream();
    // adapted the following from http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
    do
    {
        // fill the buffer with data
        count = responseStream.Read(buf, 0, buf.Length);

        // make sure we read some data
        if (count != 0)
        {
            // translate from bytes to ASCII text
            tempString = Encoding.ASCII.GetString(buf, 0, count);

            // continue building the string
            ls.Add(tempString);
        }
    }
    while (count > 0); // any more data to read?
    ArrayList myArrayList = new ArrayList();
    myArrayList.AddRange(ls);
    return myArrayList;
}

UPDATE

Yes, it did work; this test code:

private void button56_Click(object sender, EventArgs e)
{
    ArrayList al = FetchSiteQuery("http://localhost:21608/api/sitequery/getall/dbill/ppus/3");
    foreach (String s in al)
    {
        MessageBox.Show(s);
    }
}

...showed me this:

...and several more "pages" (message box instantiations) after that.

这篇关于我怎样才能解开Compact Framework的客户端上的自定义类型的返回泛型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆