2017-07-17 67 views
0

这个问题是从this问题的后续。使用RestSharp来使用RESTful API

所以,我很难让RestSharp在Xamarin.Forms PCL中工作。我的测试API具有Numero类和NumeroController类(等等)。

NÚMERO:

public class Numero 
{ 
    public int id { get; set; } 
    public int numero { get; set; } 
} 

NumeroController:

public IEnumerable<Numero> Get() 
     { 
      List<Numero> numeros = new List<Numero>(); 
      numeros.Add(new Numero { id = 1, numero = 1 }); 
      numeros.Add(new Numero { id = 2, numero = 2 }); 
      numeros.Add(new Numero { id = 3, numero = 3 }); 
      return numeros; 
     } 

在web应用程序中,用相同的NÚMERO类和一个文本框和按钮用于测试的网络表单:

using RestSharp; 

    List<Numero> numero; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 


     var restClient = new RestClient(new Uri("http://localhost:52649/")); 
     var request = new RestRequest("api/numero", Method.GET); 
     var customers = restClient.Execute<List<Numero>>(request); 
     numero = customers.Data; 
     TextBox1.Text = numero[1].numero.ToString(); 
    } 

在Xamarin.Forms PCL中,还有一个Numero类和一个按钮和标签,以及这个:

using RestSharp.Portable.HttpClient; 
    using RestSharp.Portable; 

    List<Numero> numeros; 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private async void btn_teste_Clicked(object sender, EventArgs e) 
    { 
     numeros=await GetNumeros(); 
    } 

    private async Task<List<Numero>> GetNumeros() 
    { 

     var restClient = new RestClient(new Uri("http://localhost:52649/")); 
     var request = new RestRequest("api/numero", Method.GET); 
     var customers = await restClient.Execute<List<Numero>>(request); 
     lbl_teste.Text = customers.Data.ToString(); 

     return customers.Data; 
    } 

事情是......我可以让它在web应用程序中工作得很好,但在Xamarin应用程序中,我总是得到一个异常“发生了未处理的异常。

我会很感激任何帮助。我为什么会在一个平台上工作而在另一个平台上工作而疯了。我意识到Xamarin.Forms PCL是非常严格的,但据推测,RestSharp Portable是为了这个确切目的而开发的。也许它在Xamarin上有点不同.Forms PCL?

编辑:这里的调试输出:

InspectorDebugSession(22):StateChange:开始 - > EntryPointBreakpointRegistered InspectorDebugSession(22):构建 Android应用程序正在调试。 InspectorDebugSession(22): HandleTargetEvent:TargetReady加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /App1.Android.dll 加载程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /App1.dll 加载程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /FormsViewGroup.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /Newtonsoft.Json.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /RestSharp.Portable.Core.dll [External]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /RestSharp.Portable.HttpClient.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.Animated.Vector.Drawable.dll [External]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.Design.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.v4。dll [External]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.v7.AppCompat.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.v7.CardView.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.v7.MediaRouter.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.v7.RecyclerView.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Android.Support.Vector.Drawable.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /Xamarin.Forms.Core.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Forms.Platform.Android.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /Xamarin.Forms.Platform.dll [外部]加载的程序集: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖/Xamarin.Forms.Xaml.dll [外部]加载程序集:Mono.Android.dll [外部]加载 程序集:Java.Interop.dll [外部]加载程序集: System.Runtime.dll [外部]已解决暂挂断点 'Android.Runtime.JNIEnv.RegisterJniNatives(System.IntPtr,int, System.IntPtr,System.IntPtr,int)'为[0x0](无调试符号)。加载 程序集:System.Collections.Concurrent.dll [External] Loaded 程序集:System.Threading.dll [External]加载程序集: System.Runtime.InteropServices.dll [External]加载程序集: System.Reflection.dll [外部]加载程序集: System.Collections.dll [外部]加载程序集:System.Linq.dll [外部]加载程序集:System.Core.dll [外部]加载 程序集:System.Runtime.Extensions.dll [外部]加载组件: System.Reflection.Extensions.dll [外部] InspectorDebugSession(22): HandleTargetEvent:TargetHitBreakpoint InspectorDebugSession(22): StateChange:EntryPointBreakpointRegistered - > EntryPointBreakpointHit 在spectorDebugSession(22):AgentBridge.InjectAsmbly: /storage/emulated/0/Android/data // files /。 覆盖 /inspector-temp/Xamarin.Interactive.dll InspectorDebugSession(22):AgentBridge.InjectAssembly: /storage/emulated/0/Android/data // files /。 倍率 /inspector-temp/Xamarin.Interactive.Android.dll InspectorDebugSession(22):StateChange:EntryPointBreakpointHit - > BreakdanceInvoking加载组件: /存储/模拟/ 0/Android设备/数据/文件/。 覆盖 /inspector-temp/Xamarin.Interactive.dll [外部]加载的程序集: /storage/emulated/0/Android/data/files /。 覆盖 /inspector-temp/Xamarin.Interactive.Android。dll的 [外部]线程开始:#3 InspectorDebugSession(22): HandleTargetEvent:ThreadStarted InspectorDebugSession(22): StateChange:BreakdanceInvoking - > BreakdanceStarted 07-17 01:19:56.019 I /技术(507):晚使能-Xcheck:jni 07-17 W/monodroid(507):创建公共更新目录: /data/user/0/App1.Android/files/.__override__ 07-17 01:19:56.218 W/monodroid(507):使用覆盖路径: /data /user/0/App1.Android/files/。 覆盖 07-17 01:19:56.218 W/monodroid(507):使用覆盖路径: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 07-17 01:19:56.219 W/monodroid(507):试图加载sgen: /data/user/0/App1.Android/files/。 覆盖 /libmonosgen-2.0.so 07-17 01:19:56.219 W/monodroid(507):试图加载sgen: /storage/emulated/0/Android/data/App1.Android/files/。 覆盖 /libmonosgen-2.0.so 07-17 01:19:56.219 W/monodroid(507):试图加载sgen: /data/app/App1.Android-1/lib/arm/libmonosgen-2.0 .so 07-17 01:19:56.219 W/monodroid(507):尝试加载sgen: /data/user/0/App1.Android/files/。 覆盖 /links/libmonosgen-2.0.so 07-17 01:19:56.228 W/monodroid-debug(507):尝试使用以下选项初始化 调试器: --debugger-agent = transport = dt_socket,loglevel = 0,address = 127.0.0.1:29308,server = y,embedding = 1 07-17 01:19:56.238 W/monodroid-debug(507):接受标准输出 连接:25 07-17 01:19:57.148 W/monodroid-gc(507):GREF GC 阈值:46080 07-17 01:19:57.170 W/monodroid(507):调用 管理的运行时init加载的程序集:System.ObjectModel.dll [External] Loaded assembly :System.dll [External] Loaded assembly: MonoDroidConstructors [External] 07-17 01:19:58.860 W/Mono(507): 请求t o加载程序集System.Xml.ReaderWriter v4.0.0.0被 重新映射到v2.0.5.0加载的程序集:System.Threading.Tasks.dll [External]加载的程序集:System.Xml.ReaderWriter.dll [External] 加载程序集:System.Xml.dll [External]加载程序集: System.IO.dll [External]加载程序集:System.Linq.Expressions.dll [External]加载程序集:System.Resources.ResourceManager.dll [外部]加载的程序集:System.Globalization.dll [External] Loaded 程序集:System.Dynamic.Runtime.dll [External] 07-17 01:19:59.453 W/Mono(507):加载程序集的请求 System.Xml.ReaderWriter v4.0.0.0被重新映射到v2.0.5.0 Loaded 程序集:System.Text.RegularExpressions.dll [外部]螺纹 开始:#4 InspectorDebugSession(22): HandleTargetEvent:ThreadStarted线程开始:#5 InspectorDebugSession(22):HandleTargetEvent:ThreadStarted螺纹 开始:#6 InspectorDebugSession(22): HandleTargetEvent:ThreadStarted线程开始: #7 InspectorDebugSession(22):HandleTargetEvent:ThreadStarted Loaded 程序集:System.ComponentModel.dll [External] 07-17 01:20:00.929 W/art(507):JNI RegisterNativeMethods:尝试注册0 本地方法for md5b60ffeb829f638581ab2bb9b1a7f4f3f.Platform_DefaultRenderer 07-17 01:20:00.954 W/art(507):JNI RegisterNativeMethods:尝试 注册0本地方法 md5b60ffeb829f638581ab2bb9b1a7f4f3f.LabelRenderer 07-17 01:20:01。087 W /技术(507):JNI RegisterNativeMethods:尝试注册为md5270abb39e60627f0f200893b490a1ade.ViewRenderer_2 本地方法07-17 01:20:01.184 I/HwSecImmHelper(507): mSecurityInputMethodService为null 07-17 1点20: I/OpenGLRenderer(507):初始化EGL,版本1.4 07-17 01:20(0) :01.310 W/OpenGLRenderer(507):load: so =/system/lib/libhwuibp.so 07-17 01:20:01.310 W/OpenGLRenderer( 507):dlopen failed:library“/ system/lib/libhwuibp。所以“找不到 07-17 01:20:01.310 W/OpenGLRenderer(507):初始化二进制程序 缓存:加载失败07-17 01:20:01.317 E/HAL(507):load: id = gralloc!= hmi-> id = gralloc已解决暂挂断点 'MainPage.xaml.cs:27,1'到void App1.MainPage.d__3.MoveNext()[0x0000e] 。加载组件: System.Net.Primitives.dll [外部] InspectorDebugSession(22): HandleTargetEvent:TargetHitBreakpoint InspectorDebugSession(22): HandleTargetEvent:TargetStopped InspectorDebugSession(22): HandleTargetEvent:TargetStopped加载组件: System.Diagnostics.Debug .dll [External] Loaded assembly: System.Diagnostics.Tools.dll [External] Loaded assembly: System.Net.Http.dll [External] 07-17 01:20:08.302 W/Mono(507): The请求加载组件System.Net.Http v4.0.0.0被重映射 到v2.0.5.0加载的程序集:System.Text.Encoding.dll [External] InspectorDebugSession(22):HandleTargetEvent:TargetStopped Loaded 程序集: System.Text.Encoding.Extensions.dll [External] InspectorDebugSession(22):HandleTargetEvent:TargetStopped Loaded 程序集:Mono.Security.dll [External] 07-17 01:20:10.122 I/Choreographer(507):跳过280帧!应用程序可能是 在其主线程上做了太多工作。 InspectorDebugSession(22): HandleTargetEvent:UnhandledException发生未处理的异常。

InspectorDebugSession(22):布置InspectorDebugSession(22): HandleTargetEvent:TargetExited

+1

您需要捕捉异常并检查它以确定问题所在。这是基本的C#调试。此外,不要使用本地主机 - 使用服务器的FQDN或IP – Jason

+0

嗨。忘了那个。添加尝试,赶上,但由于某种原因,它甚至没有去捕捉。它直接进入休息模式。 – taiko

+0

如果它落入IDE中,那么您应该能够查看异常 – Jason

回答

0

感谢@Jason我意识到什么地方出了错。我在对待我的网络应用程序时正在处理xamarin应用程序。该Web应用程序正在我的本地主机上进行测试,在那里我有我的RESTful API测试应用程序。所以一切都变得非常糟糕,因为它们都在我的本地主机上。

但是,由于xamarin应用程序在手机上,它不可能找到本地主机上的东西(这将在手机上)。我在服务器上测试过它,它是成功的!