2016-05-31 4 views
3

Ich habe dieses Problem nicht von Android, aber wenn ich den gleichen Arbeits Code in iOS laufen dann bekomme ich AusnahmeJson DeserializeObject wirft Ausnahme nur von iOS

public async Task Login(string inlogData, string password, string gcm) 
    { 
     Login login = new Dharma.Login(); 
     login.inlog_data = inlogData; 
     login.password = password; 
     login.phone_token = gcm; 

     HttpClient client = new HttpClient(); 
     HttpResponseMessage response = null; 

     string json = JsonConvert.SerializeObject(login, Formatting.Indented); 
     client.Timeout = TimeSpan.FromMilliseconds (10000); 
     client.BaseAddress = new Uri (ConstantVariabels.GetLoginUrl()); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     var content = new StringContent (json, Encoding.UTF8, "application/json"); 

     try 
     { 
      response = await client.PostAsync (client.BaseAddress, content); 
      response.EnsureSuccessStatusCode(); 
      var JsonResult = response.Content.ReadAsStringAsync().Result; 

      dynamic dynJson = JsonConvert.DeserializeObject(JsonResult); 
      string token = dynJson ["api_token"]; //THIS LINE THROWS THE EXCEPTION 
      LoginPageViewModel.LoginSucceeded(token, true); 
     } 
     catch(TaskCanceledException te) 
     { 
      LoginPageViewModel.LoginSucceeded(te.Message.ToString(), false); 
     } 
     catch(HttpRequestException hre) 
     { 
      LoginPageViewModel.LoginSucceeded(hre.Message.ToString(), false); 
     } 
     catch(Exception e) 
     { 
      LoginPageViewModel.LoginSucceeded("fail", false); 
     } 
    } 

Exception

Nachricht: Interpreter von ref Arten

Stacktrace:

at Microsoft.Scripting.Interpreter.LightCompiler.CompileMethodCallExpression (System.Linq.Expressions.Expression expr) [0x00089] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1283 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x001ba] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1642 
    at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileConditionalExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00007] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:818 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x001d2] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1644 
    at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
    at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00012] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1593 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileBlockExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00028] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:379 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x003d0] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1683 
    at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileGotoExpression (System.Linq.Expressions.Expression expr) [0x0001f] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:955 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x0042d] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1689 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileAsVoid (System.Linq.Expressions.Expression expr) [0x00085] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1620 
    at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00006] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1591 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileConditionalExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00071] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:828 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x001d2] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1644 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileAsVoid (System.Linq.Expressions.Expression expr) [0x00085] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1620 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileBlockStart (System.Linq.Expressions.BlockExpression node) [0x000b0] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:405 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileBlockExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00007] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:376 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x003d0] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1683 
    at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
    at Microsoft.Scripting.Interpreter.LightCompiler.CompileTop (System.Linq.Expressions.LambdaExpression node) [0x00055] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:221 
    at Microsoft.Scripting.Generation.CompilerHelpers.LightCompile (System.Linq.Expressions.LambdaExpression lambda) [0x00000] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Generation/CompilerHelpers.cs:619 
    at System.Linq.Expressions.Compiler.LambdaCompiler.Compile (System.Linq.Expressions.LambdaExpression lambda, System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator) [0x00000] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.Core/System.Linq.Expressions.Interpret/LambdaCompiler.cs:41 
    at System.Linq.Expressions.Expression`1[TDelegate].Compile() [0x00000] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/LambdaExpression.cs:188 
    at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T] (System.Runtime.CompilerServices.CallSite`1 site, System.Object[] args) [0x00059] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/CallSiteBinder.cs:148 
    at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0) [0x0010d] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/UpdateDelegates.Generated.cs:255 
    at Dharma.HttpRequests+<Login>c__async4.MoveNext() [0x0019e] in /Users/laurenspouders/dharmapp/Dharma/Library/HttpRequests.cs:345 

Antwort

5

Ich hatte das gleiche Problem und fand, dass die bisherige Newtonsoft JSON den „Wert“ standardmäßig Referenz verwendet, aber nicht jetzt, so fügen Sie einfach einen .Wert

string token = dynJson ["api_token"].Value; 
0

Das Problem ist dynJson ["api_token"] gibt den Typ Newtonsoft.Json.Linq.JTokenType.String zurück und Sie ordnen ihn string zu. Die implizite Konvertierung findet also nicht statt und gibt eine Ausnahme. Fügen Sie einen expliziten Cast hinzu, der funktioniert.

string token = (string)dynJson ["api_token"]; 

Sie müssen einen Fehler auch in Android über die implizite Besetzung erhalten.

+0

I don‘ t erhalten Fehler in Android und es hilft nicht. –

+0

Was bekommen Sie in 'dynJson'? –

+0

dynJson \t {{ „api_token“: „7Z4eV9DkaScrXkzGoWlgDHe26HyXATXKx2NBIBqHxbMtZ3kDmfU2O3FB9hk4“}} \t Newtonsoft.Json.Linq.JObject –

0

Ich löste mein Problem.

public async Task Login(string inlogData, string password, string gcm) 
    { 
     Login login = new Dharma.Login(); 
     login.inlog_data = inlogData; 
     login.password = password; 
     login.phone_token = gcm; 

     HttpClient client = new HttpClient(); 
     HttpResponseMessage response = null; 

     string json = JsonConvert.SerializeObject(login, Formatting.Indented); 
     client.Timeout = TimeSpan.FromMilliseconds (10000); 
     client.BaseAddress = new Uri (ConstantVariabels.GetLoginUrl()); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     var content = new StringContent (json, Encoding.UTF8, "application/json"); 

     try 
     { 
      response = await client.PostAsync (client.BaseAddress, content); 
      response.EnsureSuccessStatusCode(); 
      var JsonResult = response.Content.ReadAsStringAsync().Result; 

      dynamic dynJson = JsonConvert.DeserializeObject(JsonResult); 

      string token = ""; 

      foreach (var obj in dynJson) 
      { 
       if(obj.Name.Equals("api_token")) 
       { 
        token = obj.Value.ToString(); 
       } 
      } 

      LoginPageViewModel.LoginSucceeded(token, true); 
     } 
     catch(TaskCanceledException te) 
     { 
      LoginPageViewModel.LoginSucceeded(te.Message.ToString(), false); 
     } 
     catch(HttpRequestException hre) 
     { 
      LoginPageViewModel.LoginSucceeded(hre.Message.ToString(), false); 
     } 
     catch(Exception e) 
     { 
      LoginPageViewModel.LoginSucceeded("fail", false); 
     } 
    }