【问题标题】:Using RestSharp to consume a RESTful API使用 RestSharp 使用 RESTful API
【发布时间】:2017-07-17 00:15:53
【问题描述】:

此问题是this 问题的后续问题。

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

数字:

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

数字控制器:

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 应用程序中,具有相同的 Numero 类和一个带有用于测试的文本框和按钮的 Web 表单:

    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 应用程序正在调试。检查员调试会话(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 [外部] 加载程序集: /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 [外部] 加载程序集: /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 [外部] 加载程序集: /storage/emulated/0/Android/data/App1.Android/files/.override/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 [外部] 已加载 程序集:System.Threading.dll [外部] 加载程序集: System.Runtime.InteropServices.dll [外部] 加载程序集: 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 InspectorDebugSession(22):AgentBridge.InjectAssembly: /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 加载程序集: /storage/emulated/0/Android/data/files/.覆盖/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/art (507): 后期启用-Xcheck:jni 07-17 01:19:56.218 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):呼入 托管运行时初始化加载的程序集:System.ObjectModel.dll [外部] 加载程序集:System.dll [外部] 加载程序集: MonoDroidConstructors [外部] 07-17 01:19:58.860 W/Mono (507): 加载程序集 System.Xml.ReaderWriter v4.0.0.0 的请求是 重新映射到 v2.0.5.0 加载程序集:System.Threading.Tasks.dll [外部] 加载的程序集:System.Xml.ReaderWriter.dll [外部] 加载的程序集:System.Xml.dll [外部] 加载的程序集: System.IO.dll [外部] 加载的程序集:System.Linq.Expressions.dll [外部] 加载程序集:System.Resources.ResourceManager.dll [外部] 加载程序集:System.Globalization.dll [外部] 加载 程序集:System.Dynamic.Runtime.dll [外部] 07-17 01:19:59.453 W/Mono(507):加载程序集的请求 System.Xml.ReaderWriter v4.0.0.0 已重新映射到 v2.0.5.0 已加载 程序集: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 [外部] 07-17 01:20:00.929 W/art (507): JNI RegisterNativeMethods: 尝试注册 0 本机方法 md5b60ffeb829f638581ab2bb9b1a7f4f3f.Platform_DefaultRenderer 07-17 01:20:00.954 W/art (507): JNI RegisterNativeMethods: 尝试 注册 0 个本地方法 md5b60ffeb829f638581ab2bb9b1a7f4f3f.LabelRenderer 07-17 01:20:01.087 W/art (507): JNI RegisterNativeMethods: 尝试注册 0 md5270abb39e60627f0f200893b490a1ade.ViewRenderer_2 的本机方法 07-17 01:20:01.184 I/HwSecImmHelper(507): mSecurityInputMethodService 为空 07-17 01:20:01.297 E/HAL ( 507): 加载: id=gralloc != hmi->id=gralloc 07-17 01:20:01.298 I/OpenGLRenderer(507):初始化 EGL,版本 1.4 07-17 01:20:01.310 W/OpenGLRenderer(507):加载: 所以=/system/lib/libhwuibp.so 07-17 01:20:01.310 W/OpenGLRenderer( 507):dlopen 失败:找不到库“/system/lib/libhwuibp.so” 07-17 01:20:01.310 W/OpenGLRenderer(507):初始化二进制程序 缓存:加载失败 07-17 01:20:01.317 E/HAL(507):加载: id=gralloc != hmi->id=gralloc 解决挂起断点在 'MainPage.xaml.cs:27,1' 无效 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 [外部] 加载程序集: System.Diagnostics.Tools.dll [外部] 加载程序集: System.Net.Http.dll [外部] 07-17 01:20:08.302 W/Mono (507): 重新映射了加载程序集 System.Net.Http v4.0.0.0 的请求 到 v2.0.5.0 加载程序集:System.Text.Encoding.dll [外部] InspectorDebugSession(22): HandleTargetEvent: TargetStopped Loaded 程序集:System.Text.Encoding.Extensions.dll [外部] InspectorDebugSession(22): HandleTargetEvent: TargetStopped Loaded 程序集:Mono.Security.dll [外部] 07-17 01:20:10.122 I/Choreographer(507):跳过280帧!该应用程序可能是 在其主线程上做太多工作。检查员调试会话(22): HandleTargetEvent: UnhandledException 发生未处理的异常。

InspectorDebugSession(22):已处置 InspectorDebugSession(22): HandleTargetEvent: TargetExited

【问题讨论】:

  • 您需要捕获异常并检查它以确定问题所在。这是基本的 C# 调试。此外,不要使用 localhost - 使用 FQDN 或服务器的 IP
  • 嗨。忘了那个。添加了 try,catch 但由于某种原因它甚至没有去捕获。它直接进入中断模式。
  • 如果它进入 IDE,那么您应该能够查看异常
  • 你的意思是调试输出?如果是这样,我将其添加到主帖中。不知道有没有用。
  • 没有。如果在 VS 中出现“未处理的异常”对话框,您应该能够选择“中断”并进入调试器并查看异常详细信息。

标签: c# asp.net asp.net-web-api xamarin.forms


【解决方案1】:

感谢@Jason,我意识到出了什么问题。我在对待我的网络应用程序时对待 xamarin 应用程序。 Web 应用程序正在我的本地主机上进行测试,在那里我有我的 RESTful API 测试应用程序。所以一切都很顺利,因为它们都在我的本地主机上。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多