使用System.Net.WebClient时System.Threading.Tasks.Task.Schedule中的NullReferenceException

时间:2015-04-21 15:25:01

标签: c# android xamarin webclient nullreferenceexception

我发布了another post同样的问题。在github上有一个简单的Xamarin解决方案,您可以下载并运行here。请做。

似乎在我的Xperia Z3上以DEBUG模式调用System.Net.WebClient.DownloadDataTaskAsync(它可以与模拟器一起使用)第一次在NullReferenceException中抛出System.Threading.Tasks.Task.Schedule

    public async Task<String> DownloadString(String url)
    {
        if (UseAuth)
            webclient.Headers.Set("Authorization", TokenType + " " + AccessToken);
        else if (!UseAuth && webclient.Headers["Authorization"] != null)
            webclient.Headers.Remove("Authorization");
        String response = "";

        try
        {
            byte[] data = await webclient.DownloadDataTaskAsync(url);
            response = Encoding.UTF8.GetString(data);
        }
        catch (WebException e)
        {
            response = new StreamReader (e.Response.GetResponseStream ()).ReadToEnd ();
            ErrorResponse = JsonConvert.DeserializeObject<ErrorResponse> (response, settings);

            if (ErrorResponse.Error.Status == 401 && ErrorResponse.Error.Message == "The access token expired") {
                Console.WriteLine ("Error: " + ErrorResponse.Error.Status + " - " + ErrorResponse.Error.Message);
            }
        }
        return response;
    }

此代码解决了这个问题,但我认为这是一个肮脏的修复,并没有真正解决潜在的问题。

public async Task<String> DownloadString(String url)
    {
        if (UseAuth)
            webclient.Headers.Set("Authorization", TokenType + " " + AccessToken);
        else if (!UseAuth && webclient.Headers["Authorization"] != null)
            webclient.Headers.Remove("Authorization");
        String response = "";

        var failCount = 2;
        for (int i = 0; i <= failCount; i++) 
        {
            try
            {
                byte[] data = await webclient.DownloadDataTaskAsync(url);
                response = Encoding.UTF8.GetString(data);
            }
            catch (WebException e)
            {
                if (i == failCount) 
                {
                    response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();

                    ErrorResponse = JsonConvert.DeserializeObject<ErrorResponse>(response, settings);

                    if (ErrorResponse.Error.Status == 401 && ErrorResponse.Error.Message == "The access token expired") 
                    {
                        Console.WriteLine ("Error: " + ErrorResponse.Error.Status + " - " + ErrorResponse.Error.Message);
                    }
                    // Break out of the loop
                    break;
                }
            }

        }

        return response;
    }
  

System.NullReferenceException:未将对象引用设置为实例   一个对象System.Threading.Tasks.Task.Schedule(布尔值   throwException)[0x00000] in:在System.Threading.Tasks.Task.Start中为0   (System.Threading.Tasks.TaskScheduler scheduler)[0x00000] in:0 at   System.Threading.Tasks.TaskFactory.StartNew [WebRequest](System.Func1   function,CancellationToken cancellationToken,TaskCreationOptions   creationOptions,System.Threading.Tasks.TaskScheduler scheduler)   [0x00000] in:0 at   System.Threading.Tasks.TaskFactory.StartNew [WebRequest](System.Func1   函数)[0x00000] in:System.Net.WebClient.SetupRequestAsync中的0   (System.Uri地址)[0x00000] in:0 at   System.Net.WebClient + c__async0.MoveNext()[0x00000] in:0 --- End of   内部异常堆栈跟踪--- at   System.Net.WebClient + c__async0.MoveNext()[0x00000] in:0 --- End of   从抛出异常的先前位置堆栈跟踪--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   [0x00000] in:0 at   System.Runtime.CompilerServices.TaskAwaiter`1 [System.Byte []]。调用getResult   ()[0x00000] in:0 at   SpotifyWebAPI.SpotifyWebAPIClass + c__async1A.MoveNext()[0x00133]   System.Net.WebException

以下是Xamarin Studio的截图

enter image description here

问题在我的设备和DEBUG构建模式下发生。在RELEASE中,它完美地工作,并且在模拟器上它可以在DEBUG和RELEASE上工作。

正如原帖中所述,第二次,第三次或第N次调用System.WebClient.DownloadDataTaskAsync都会导致正常行为。它也只发生在我使用Spotify Android SDK Java绑定项目并在调用OpenLoginWindow()之前调用DownloadDataTaskAsync(打开另一个活动,登录并成功返回)时。

0 个答案:

没有答案