本文介绍了MonoDroid:Encoding.ASCII.GetString失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

间断地,Encoding.ASCII.GetString调用失败,异常转义所有的catch块并冻结应用程序。

 私有字符串ExecuteRequest(Uri url,KeyValuePair< string,string> [] postItems = null)
{
var data = new byte [0];
var response = new byte [0];
使用(var client = new WebClient())
{
if(postItems!= null&& postItems.Count()> 0)
{
string dataString = string.Join(&,postItems.Select(
item => string.Format({0} = {1},item.Key,item.Value))ToArray ());
data = new ASCIIEncoding()。GetBytes(dataString);
}
response = client.UploadData(url,POST,data);
Android.Util.Log.Info(info,收到的帖子的响应,即将获取字符串);
client.Dispose();
}
try
{
return Encoding.ASCII.GetString(response);
}
catch(Exception ex)
{
Android.Util.Log.Info(info,
Encoding.ASCII.GetString Exception:{0} ,{1},ex.Message,ex.StackTrace);
抛出新的ApplicationException(UnRecoverable。Abort);
}
}

StackTrace我得到的是

  I / sssvida(10960):收到的帖子的回复。 (10960):
I / mono(10960):
I / mono(10960):在System.Text.ASCIIEncoding.GetString(byte [ ],int,int)< 0x000cb>
I / mono(10960):在System.Text.Encoding.GetString(byte [])< 0x00037>
I / mono(10960):在ServiceRequest.ExecuteRequest(System.Uri,System.Collections.Generic.KeyValuePair`2< string,string> [])< 0x0026b>

从某种意义上说,我正在获得如下所示的无效堆栈跟踪



$ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ./../../mono/mini/mini-exceptions.c:472,条件`class'不符合
D / dalvikvm(220):GC_EXPLICIT释放1K,35%可用17547K / 26759K,暂停3ms + 3ms

响应是可以在1 - 4 mb之间的json数据。



请帮忙。谢谢!!!!



编辑2
我更新了代码以使用UploadString而不是UploadData,间歇性地得到这样的代码:

  I / mono(15065):StackTrace:
I / mono(15065):
I / mono(15065):at string.CreateString(char [])< 0x0004b>
I / mono(15065):at(wrapper managed-to-managed)string..ctor(char [])< 0xffffffff>
I / mono(15065):在System.Text.Encoding.GetString(byte [],int,int)< 0x00043>
I / mono(15065):在System.Text.UTF8Encoding.GetString(byte [],int,int)< 0x0002b>
I / mono(15065):在System.Text.Encoding.GetString(byte [])< 0x00037>
I / mono(15065):System.Net.WebClient.UploadString(System.Uri,string,string)< 0x0007f>
I / mono(15065):at(wrapper remoting-invoke-with-check)System.Net.WebClient.UploadString(System.Uri,string,string)< 0xffffffff>


解决方案

分页数据和大块下载可能是唯一正确的和可扩展的/可靠的解决方案。



现在,下面的代码不是很大,可能是我没有碰到转折点。但这是我第一次通过设备上的分块。 Encoding.ASCII.GetString不会在下面的代码中出现。

 私有字符串ExecuteRequest(Uri url,KeyValuePair< string,string> ; [] postItems = null)
{
var data = new byte [0];
var response = new byte [0];
///切换到WebClient,因为
/// http://stackoverflow.com/questions/8167726/monodroid-intermittent-failure-when-reading-a-large-json-string-from- web-servi
使用(var client = new WebClient())
{
if(postItems!= null&& postItems.Count()> 0)
{
string dataString = string.Join(&,postItems.Select(
item => string.Format({0} = {1},item.Key,item.Value) ).ToArray());
data = new ASCIIEncoding()。GetBytes(dataString);
}
response = client.UploadData(url,POST,data);
Android.Util.Log.Info(info,收到的帖子的响应,即将获取字符串);
client.Dispose();
}
尝试
{
Android.Util.Log.Info(info,response size:{0},response.Length);
var chunkSize = 50000;
if(response.Length> chunkSize)
{
var returnValue = new StringBuilder(); (int i = 0; i< response.Length; i + = chunkSize)

{
int end =(i + chunkSize)>响应长度? response.Length - i:chunkSize;
returnValue.Append(Encoding.ASCII.GetString(response,i,end));
Android.Util.Log.Info(info,从{0}添加到{1},我结束);
}
return returnValue.ToString();
}
return Encoding.ASCII.GetString(response);
}
catch(Exception ex)
{
Android.Util.Log.Info(info,
Encoding.ASCII.GetString Exception:{0} ,{1},ex.Message,ex.StackTrace);
抛出新的ApplicationException(UnRecoverable。Abort);
}
}


Intermittently, Encoding.ASCII.GetString call fails with an exception that escapes all the catch blocks in place and freezes the app.

private string ExecuteRequest(Uri url, KeyValuePair<string, string>[] postItems = null)
{
    var data = new byte[0];
    var response = new byte[0];
    using (var client = new WebClient())
    {
        if (postItems != null && postItems.Count() > 0)
        {
            string dataString = string.Join("&", postItems.Select(
                                    item => string.Format("{0}={1}", item.Key, item.Value)).ToArray());
            data = new ASCIIEncoding().GetBytes(dataString);
        }
        response = client.UploadData(url, "POST", data);
        Android.Util.Log.Info("info", "response from the post received. about to get string");
        client.Dispose();
    }
    try
    {
        return Encoding.ASCII.GetString(response);
    }
    catch (Exception ex)
    {
        Android.Util.Log.Info("info",
            "Encoding.ASCII.GetString Exception : {0}, {1}", ex.Message, ex.StackTrace);
        throw new ApplicationException("UnRecoverable. Abort");
    }
}

StackTrace I get is

I/sssvida (10960): response from the post received. about to get string
I/mono    (10960): Stacktrace:
I/mono    (10960):
I/mono    (10960):   at System.Text.ASCIIEncoding.GetString (byte[],int,int) <0x000cb>
I/mono    (10960):   at System.Text.Encoding.GetString (byte[]) <0x00037>
I/mono    (10960):   at ServiceRequest.ExecuteRequest (System.Uri,System.Collections.Generic.KeyValuePair`2<string, string>[]) <0x0026b>

Intermitently, I am getting an unusal stacktrace shown below

I/mono    ( 9817): Stacktrace:
I/mono    ( 9817):
F/        ( 9817): * Assertion at ../../../../mono/mini/mini-exceptions.c:472, condition `class' not met
D/dalvikvm(  220): GC_EXPLICIT freed 1K, 35% free 17547K/26759K, paused 3ms+3ms

The response is json data that can range between 1 - 4 mb.

Please help. Thanks!!!!

Edit 2 :I updated the code to use UploadString instead of UploadData and intermittently I get this :

I/mono    (15065): Stacktrace:
I/mono    (15065):
I/mono    (15065):   at string.CreateString (char[]) <0x0004b>
I/mono    (15065):   at (wrapper managed-to-managed) string..ctor (char[]) <0xffffffff>
I/mono    (15065):   at System.Text.Encoding.GetString (byte[],int,int) <0x00043>
I/mono    (15065):   at System.Text.UTF8Encoding.GetString (byte[],int,int) <0x0002b>
I/mono    (15065):   at System.Text.Encoding.GetString (byte[]) <0x00037>
I/mono    (15065):   at System.Net.WebClient.UploadString (System.Uri,string,string) <0x0007f>
I/mono    (15065):   at (wrapper remoting-invoke-with-check) System.Net.WebClient.UploadString (System.Uri,string,string) <0xffffffff>
解决方案

Paging data and downloading in chunks might be the only correct and scalable/robust solution here.

For now, the code below is not blowing up, it may be that I have not hit the tipping point. But this is my first pass at chunking on the device. Encoding.ASCII.GetString does not blowing up in the code below.

private string ExecuteRequest(Uri url, KeyValuePair<string, string>[] postItems = null)
{
    var data = new byte[0];
    var response = new byte[0];
    ///switched to WebClient because
    /// http://stackoverflow.com/questions/8167726/monodroid-intermittent-failure-when-reading-a-large-json-string-from-web-servi
    using (var client = new WebClient())
    {
        if (postItems != null && postItems.Count() > 0)
        {
            string dataString = string.Join("&", postItems.Select(
                                    item => string.Format("{0}={1}", item.Key, item.Value)).ToArray());
            data = new ASCIIEncoding().GetBytes(dataString);
        }
        response = client.UploadData(url, "POST", data);
        Android.Util.Log.Info("info", "response from the post received. about to get string");
        client.Dispose();
    }
    try
    {
        Android.Util.Log.Info("info", "response size : {0}", response.Length);
        var chunkSize = 50000;
        if (response.Length > chunkSize)
        {
            var returnValue = new StringBuilder();
            for (int i = 0; i < response.Length; i+= chunkSize)
            {
                int end = (i + chunkSize) > response.Length ? response.Length - i : chunkSize;
                returnValue.Append(Encoding.ASCII.GetString(response, i, end));
                Android.Util.Log.Info("info", "added a chunk from {0} to {1}", i, end);
            }
            return returnValue.ToString();
        }
        return Encoding.ASCII.GetString(response);
    }
    catch (Exception ex)
    {
        Android.Util.Log.Info("info",
            "Encoding.ASCII.GetString Exception : {0}, {1}", ex.Message, ex.StackTrace);
        throw new ApplicationException("UnRecoverable. Abort");
    }
}

这篇关于MonoDroid:Encoding.ASCII.GetString失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:29