MonoDroid:Encoding.ASCII.GetString 失败
Encoding.ASCII.GetString 调用间歇性地失败,并出现异常,该异常转义了所有 catch 块并冻结了应用程序。
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/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>
间歇性的,我得到了一个不寻常的堆栈跟踪,如下所示。
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
响应是 json 数据,范围在 1 - 4 mb 之间。
请帮忙。谢谢!!!!
编辑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): 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请按照 Rolf 在评论中的要求填写错误报告。第二个堆栈跟踪是永远不应该发生的崩溃。
至于第一个问题,有什么理由不使用(其中一个重载) WebClient.UploadString ?
该方法将负责编码,确保 HTTP 请求标头匹配,对请求和响应进行编码...这还将返回一个字符串(为您提供更少的代码:-)。
否则,您最终可能会得到一个无法用 ASCII 编码的字符串(除非您在此代码之前进行了其他验证),并且会导致异常(遗憾的是我们只有堆栈跟踪,而不是抛出的确切异常来确认这一点)。
First, please fill a bug report as Rolf asked in the comments. The second stack trace is a crash that should never occur.
As for the first issue, is there any reason not to use (one of the overload) WebClient.UploadString ?
That method will take care of the encoding, ensuring the HTTP request header match, encoding the request and response... which will also return you a string (less code for you :-).
Otherwise you could end up with a string (unless you have other validations prior to this code) that cannot be encoded in ASCII and that would cause exceptions (sadly we only have the stack trace, not the exact exception being thrown to confirm this).
分页数据和分块下载可能是这里唯一正确且可扩展/稳健的解决方案。
目前来看,下面的代码还没有炸毁,可能是我还没有达到临界点。但这是我第一次在设备上进行分块。 Encoding.ASCII.GetString 在下面的代码中不会崩溃。
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.