Monodroid WebRequest 炸弹应用程序

发布于 2024-12-06 19:39:40 字数 693 浏览 0 评论 0原文

我已经在普通的 C# 应用程序中尝试过这段代码,它工作得很好。在 monodroid 中,当我尝试以任何方式从流(或基本流)中读取时,它完全出错(换句话说,甚至 try-catch 都不起作用)。请帮助:

try
{
    WebRequest request = WebRequest.Create(string.Format("http://maps.google.com/maps/geo?q={0},{1}&output=xml&sensor=false", "35.245619","-98.276369"));
    WebResponse wresponse = request.GetResponse();

    using (StreamReader sr = new StreamReader(wresponse.GetResponseStream()))
    {
        RunOnUiThread(() => _debug.Text = (sr.ReadToEnd()).ToString());
    }
    wresponse.Close();
}
catch (Exception ex)
{
    RunOnUiThread(() => _debug.Text = string.Format("Exception: ", ex.Message));
}

_debug 是我的 UI 上的 TextView 对象。

I have tried this code in a normal C# app and it works fine. In monodroid, it completely bugs out (in other words, not even the try-catch works) when I try to READ from the stream (or the base stream) in ANY way. Please help:

try
{
    WebRequest request = WebRequest.Create(string.Format("http://maps.google.com/maps/geo?q={0},{1}&output=xml&sensor=false", "35.245619","-98.276369"));
    WebResponse wresponse = request.GetResponse();

    using (StreamReader sr = new StreamReader(wresponse.GetResponseStream()))
    {
        RunOnUiThread(() => _debug.Text = (sr.ReadToEnd()).ToString());
    }
    wresponse.Close();
}
catch (Exception ex)
{
    RunOnUiThread(() => _debug.Text = string.Format("Exception: ", ex.Message));
}

_debug is a TextView object on my UI.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-12-13 19:39:40

这条路怎么样?

try
{
    WebRequest request = WebRequest.Create(string.Format("http://maps.google.com/maps/geo?q={0},{1}&output=xml&sensor=false", "35.245619","-98.276369"));
    WebResponse wresponse = request.GetResponse();
    var resp=string.Empty;
    using (StreamReader sr = new StreamReader(wresponse.GetResponseStream()))
    {
        resp=sr.ReadToEnd().ToString();
    }
    wresponse.Close();
    RunOnUiThread(() => _debug.Text = resp);
}
catch (Exception ex)
{
    RunOnUiThread(() => _debug.Text = string.Format("Exception: ", ex.Message));
}

How about this way?

try
{
    WebRequest request = WebRequest.Create(string.Format("http://maps.google.com/maps/geo?q={0},{1}&output=xml&sensor=false", "35.245619","-98.276369"));
    WebResponse wresponse = request.GetResponse();
    var resp=string.Empty;
    using (StreamReader sr = new StreamReader(wresponse.GetResponseStream()))
    {
        resp=sr.ReadToEnd().ToString();
    }
    wresponse.Close();
    RunOnUiThread(() => _debug.Text = resp);
}
catch (Exception ex)
{
    RunOnUiThread(() => _debug.Text = string.Format("Exception: ", ex.Message));
}
墨小沫ゞ 2024-12-13 19:39:40

声音已经给出了答案。那应该有效。我稍微解释一下原因。

从您的代码来看,您似乎是在后台线程上执行 HTTP 请求。这就是为什么您需要执行 RunOnUiThread。这是一个非常好的做法。

但是,RunOnUiThread 并不保证代码会立即在 UI 线程上执行。它只是向 UI 线程运行循环发送一条消息。当UI线程有机会时,它就会执行它。

这本质上意味着“wresponse.close()”可能会在“resp=sr.ReadToEnd().ToString()”之前运行。由于响应是关闭的,任何读取响应的尝试都会导致错误。但错误发生在 UI 线程上,因为读取尝试将在 UI 线程上进行。这就是为什么你的 try/catch 块不起作用。

在Sound的代码中,这个问题被消除了。附带说明一下,由于字节的实际读取被卸载到工作线程,因此该代码的性能也更好,因此您的 UI 线程的响应速度会更快。

Sound has provided the answer. That should work. I'll just explain the reason a bit.

From your code, it seems like you are doing the HTTP request on a background thread. That's why you need to do the RunOnUiThread. This is a very good approach.

However, RunOnUiThread does not guarantee that the code will be executed immediately on the UI thread. It merely posts a message to the UI thread run loop. And when the UI thread gets a chance, it will execute it.

What this essentially means is that "wresponse.close()" will probably run before "resp=sr.ReadToEnd().ToString()". Since the response is closed, any attempt to read from it will cause an error. But the error happens on the UI thread since the read attempt will be on UI thread. Thats why your try/catch block doesn't function.

In Sound's code, this problem is eliminated. As a side note, this code is also much better performing since the actual reading of bytes is offloaded to the worker thread, so your UI thread will be much more responsive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文