本文介绍了MonoDroid的:读从Web服务的大型JSON字符串时,间歇性故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个线程(显示UI进度对话框)低于code从ASP.Net MVC的Web服务读取JSON字符串。该数据可以是1 2MB和4MB之间

I use the code below on a thread (showing a progress dialog in ui) to read json string from a ASP.Net MVC web service. The data can be between 1 mb and 4 mb.

    public static class WebRequestEx
    {
        public static string ExecuteRequestReadToEnd(this WebRequest req)
        {
            var resp = req.GetResponse();
            using (var resps = resp.GetResponseStream())
            {
                StringBuilder sb = new StringBuilder();
                // read through the stream loading up the string builder
                using (var respRdr = new StreamReader(resps))
                {
                    //return respRdr.ReadToEnd();
                    while (!respRdr.EndOfStream)
                    {
                        sb.Append(respRdr.ReadLine());
                    }
                    return sb.ToString();
                }
            }
        }
}

堆栈跟踪如下:

I/mono    (15666): Stacktrace:
I/mono    (15666):
I/mono    (15666):   at System.Threading.WaitHandle.set_Handle (intptr) <0x0008b>
I/mono    (15666):   at System.Threading.EventWaitHandle..ctor (bool,System.Threading.EventResetMode) <0x00053>
I/mono    (15666):   at System.Threading.ManualResetEvent..ctor (bool) <0x0001f>
I/mono    (15666):   at (wrapper remoting-invoke-with-check) System.Threading.ManualResetEvent..ctor (bool) <0xffffffff>
I/mono    (15666):   at System.Net.WebAsyncResult.get_AsyncWaitHandle () <0x00073>
I/mono    (15666):   at System.Net.WebAsyncResult.WaitUntilComplete (int,bool) <0x00033>
I/mono    (15666):   at System.Net.WebConnectionStream.Read (byte[],int,int) <0x000b3>
I/mono    (15666):   at System.IO.StreamReader.ReadBuffer () <0x00047>
I/mono    (15666):   at System.IO.StreamReader.ReadLine () <0x0014b>
I/mono    (15666):   at System.WebRequestEx.ExecuteRequestReadToEnd (System.Net.WebRequest) <0x000ab>

的问题是,这种情况不会发生,每次和是间歇性的。这是不好的,因为它会导致整个应用程序冻结和进度对话框被套牢用户无法做到与应用东西。我有在调用code一个try / catch块,但这似乎例外避开它。

The issue is that this does not happen every time and is intermittent. This is bad because it causes the whole app to freeze and progress dialog is stuck with the user unable to do anything with the app. I do have a try/catch block in the calling code but this exception seems to get around it.

我前面用ReadToEnd的和被炸毁间歇性,以及让我切换到逐行读取一行。

I was using ReadToEnd earlier and that was blowing up intermittently as well so I switched to reading line by line.

堆栈跟踪不是特别有益,因为服用点似乎内的ReadLine()。

The stack trace is not particularly helpful as somehting seems to be going on within Readline().

思考/咨询/替代品?

推荐答案

除非你有你的的WebRequest 你应该使用的和(甚至更好) DownloadStringAsync 将会保护你处理响应位,其中可以很容易地分配办法不多内存和临时串,这将影响应用程序的性能

Methods like DownloadString and (even better) DownloadStringAsync will shield you from processing the response bits where it's easy to allocate way to much memory and temporary strings that will affect your application performance.

这篇关于MonoDroid的:读从Web服务的大型JSON字符串时,间歇性故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:06