2017-07-17 31 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應用程序在手機上,它不可能找到本地主機上的東西(這將在手機上)。我在服務器上測試過它,它是成功的!