1

this question is a followup from this question.

So, I'm having a hard time getting RestSharp to work in Xamarin.Forms PCL. My test API has the Numero class and the NumeroController class (amongst others).

Numero:

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;
        }

In a web application, with the same Numero class and a webform with a textbox and button for testing:

    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();
    }

And in the Xamarin.Forms PCL, with a Numero class and a button and label as well, and this:

    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;
    }

The thing is... I can get it to work just fine on the web app, but on the Xamarin app I always get an exception "An unhandled exception occured."

I'd appreciate any help given. I'm going crazy as to why this works on one platform and nt the other. I realize the Xamarin.Forms PCL is very restrictive but supposedly the RestSharp Portable was developed for this exact purpose. Perhaps it works a bit differently on Xamarin.Forms PCL?

EDIT: Here's the debug output:

InspectorDebugSession(22): StateChange: Start -> EntryPointBreakpointRegistered InspectorDebugSession(22): Constructed Android application is debugging. InspectorDebugSession(22): HandleTargetEvent: TargetReady Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/App1.Android.dll Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/App1.dll Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/FormsViewGroup.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Newtonsoft.Json.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/RestSharp.Portable.Core.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/RestSharp.Portable.HttpClient.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.Animated.Vector.Drawable.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.Design.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.v4.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.v7.AppCompat.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.v7.CardView.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.v7.MediaRouter.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.v7.RecyclerView.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Android.Support.Vector.Drawable.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Forms.Core.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Forms.Platform.Android.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Forms.Platform.dll [External] Loaded assembly: /storage/emulated/0/Android/data/App1.Android/files/.override/Xamarin.Forms.Xaml.dll [External] Loaded assembly: Mono.Android.dll [External] Loaded assembly: Java.Interop.dll [External] Loaded assembly: System.Runtime.dll [External] Resolved pending breakpoint for 'Android.Runtime.JNIEnv.RegisterJniNatives(System.IntPtr, int, System.IntPtr, System.IntPtr, int)' to [0x0](no debug symbols). Loaded assembly: System.Collections.Concurrent.dll [External] Loaded assembly: System.Threading.dll [External] Loaded assembly: System.Runtime.InteropServices.dll [External] Loaded assembly: System.Reflection.dll [External] Loaded assembly: System.Collections.dll [External] Loaded assembly: System.Linq.dll [External] Loaded assembly: System.Core.dll [External] Loaded assembly: System.Runtime.Extensions.dll [External] Loaded assembly: System.Reflection.Extensions.dll [External] InspectorDebugSession(22): HandleTargetEvent: TargetHitBreakpoint InspectorDebugSession(22): StateChange: EntryPointBreakpointRegistered -> EntryPointBreakpointHit InspectorDebugSession(22): AgentBridge.InjectAssembly: /storage/emulated/0/Android/data//files/.override/inspector-temp/Xamarin.Interactive.dll InspectorDebugSession(22): AgentBridge.InjectAssembly: /storage/emulated/0/Android/data//files/.override/inspector-temp/Xamarin.Interactive.Android.dll InspectorDebugSession(22): StateChange: EntryPointBreakpointHit -> BreakdanceInvoking Loaded assembly: /storage/emulated/0/Android/data/files/.override/inspector-temp/Xamarin.Interactive.dll [External] Loaded assembly: /storage/emulated/0/Android/data/files/.override/inspector-temp/Xamarin.Interactive.Android.dll [External] Thread started: #3 InspectorDebugSession(22): HandleTargetEvent: ThreadStarted InspectorDebugSession(22): StateChange: BreakdanceInvoking -> BreakdanceStarted 07-17 01:19:56.019 I/art ( 507): Late-enabling -Xcheck:jni 07-17 01:19:56.218 W/monodroid( 507): Creating public update directory: /data/user/0/App1.Android/files/.__override__ 07-17 01:19:56.218 W/monodroid( 507): Using override path: /data/user/0/App1.Android/files/.override 07-17 01:19:56.218 W/monodroid( 507): Using override path: /storage/emulated/0/Android/data/App1.Android/files/.override 07-17 01:19:56.219 W/monodroid( 507): Trying to load sgen from: /data/user/0/App1.Android/files/.override/libmonosgen-2.0.so 07-17 01:19:56.219 W/monodroid( 507): Trying to load sgen from: /storage/emulated/0/Android/data/App1.Android/files/.override/libmonosgen-2.0.so 07-17 01:19:56.219 W/monodroid( 507): Trying to load sgen from: /data/app/App1.Android-1/lib/arm/libmonosgen-2.0.so 07-17 01:19:56.219 W/monodroid( 507): Trying to load sgen from: /data/user/0/App1.Android/files/.override/links/libmonosgen-2.0.so 07-17 01:19:56.228 W/monodroid-debug( 507): Trying to initialize the debugger with options: --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): Accepted stdout connection: 25 07-17 01:19:57.148 W/monodroid-gc( 507): GREF GC Threshold: 46080 07-17 01:19:57.170 W/monodroid( 507): Calling into managed runtime init Loaded assembly: System.ObjectModel.dll [External] Loaded assembly: System.dll [External] Loaded assembly: MonoDroidConstructors [External] 07-17 01:19:58.860 W/Mono ( 507): The request to load the assembly System.Xml.ReaderWriter v4.0.0.0 was remapped to v2.0.5.0 Loaded assembly: System.Threading.Tasks.dll [External] Loaded assembly: System.Xml.ReaderWriter.dll [External] Loaded assembly: System.Xml.dll [External] Loaded assembly: System.IO.dll [External] Loaded assembly: System.Linq.Expressions.dll [External] Loaded assembly: System.Resources.ResourceManager.dll [External] Loaded assembly: System.Globalization.dll [External] Loaded assembly: System.Dynamic.Runtime.dll [External] 07-17 01:19:59.453 W/Mono ( 507): The request to load the assembly System.Xml.ReaderWriter v4.0.0.0 was remapped to v2.0.5.0 Loaded assembly: System.Text.RegularExpressions.dll [External] Thread started: #4 InspectorDebugSession(22): HandleTargetEvent: ThreadStarted Thread started: #5 InspectorDebugSession(22): HandleTargetEvent: ThreadStarted Thread started: #6 InspectorDebugSession(22): HandleTargetEvent: ThreadStarted Thread started: #7 InspectorDebugSession(22): HandleTargetEvent: ThreadStarted Loaded assembly: System.ComponentModel.dll [External] 07-17 01:20:00.929 W/art ( 507): JNI RegisterNativeMethods: attempt to register 0 native methods for md5b60ffeb829f638581ab2bb9b1a7f4f3f.Platform_DefaultRenderer 07-17 01:20:00.954 W/art ( 507): JNI RegisterNativeMethods: attempt to register 0 native methods for md5b60ffeb829f638581ab2bb9b1a7f4f3f.LabelRenderer 07-17 01:20:01.087 W/art ( 507): JNI RegisterNativeMethods: attempt to register 0 native methods for md5270abb39e60627f0f200893b490a1ade.ViewRenderer_2 07-17 01:20:01.184 I/HwSecImmHelper( 507): mSecurityInputMethodService is null 07-17 01:20:01.297 E/HAL ( 507): load: id=gralloc != hmi->id=gralloc 07-17 01:20:01.298 I/OpenGLRenderer( 507): Initialized EGL, version 1.4 07-17 01:20: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.so" not found 07-17 01:20:01.310 W/OpenGLRenderer( 507): Initialize Binary Program Cache: Load Failed 07-17 01:20:01.317 E/HAL ( 507): load: id=gralloc != hmi->id=gralloc Resolved pending breakpoint at 'MainPage.xaml.cs:27,1' to void App1.MainPage.d__3.MoveNext () [0x0000e]. Loaded assembly: System.Net.Primitives.dll [External] InspectorDebugSession(22): HandleTargetEvent: TargetHitBreakpoint InspectorDebugSession(22): HandleTargetEvent: TargetStopped InspectorDebugSession(22): HandleTargetEvent: TargetStopped Loaded assembly: 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 request to load the assembly System.Net.Http v4.0.0.0 was remapped to v2.0.5.0 Loaded assembly: System.Text.Encoding.dll [External] InspectorDebugSession(22): HandleTargetEvent: TargetStopped Loaded assembly: System.Text.Encoding.Extensions.dll [External] InspectorDebugSession(22): HandleTargetEvent: TargetStopped Loaded assembly: Mono.Security.dll [External] 07-17 01:20:10.122 I/Choreographer( 507): Skipped 280 frames! The application may be doing too much work on its main thread. InspectorDebugSession(22): HandleTargetEvent: UnhandledException An unhandled exception occured.

InspectorDebugSession(22): Disposed InspectorDebugSession(22): HandleTargetEvent: TargetExited

6
  • 1
    you need to catch the exception and examine it to determine what the problem is. This is basic C# debugging. Further, don't use localhost - use either the FQDN or the IP of the server Commented Jul 17, 2017 at 0:17
  • Hi. Forgot about that. Added try,catch but for some reason it doesn't even go to the catch. It goes straight to break mode. Commented Jul 17, 2017 at 0:21
  • If it drops into the IDE then you should be able to view the exception Commented Jul 17, 2017 at 0:23
  • You mean the debug output? If so, I added it to the main post. I don't know if it will help. Commented Jul 17, 2017 at 0:25
  • No. If you get the "Unhandled Exception" dialog in VS, you should be able to select "Break" and drop into the debugger and view the exception details. Commented Jul 17, 2017 at 0:37

1 Answer 1

0

Thanks to @Jason I realised what went wrong. I was treating the xamarin app as I was treating my web app. The web app was being tested on my localhost, where I had my RESTful API test app. So everything went smothly because they were both on my localhost.

But since the xamarin app is on a phone it can't possibly find something on the localhost (which would be on the phone). I tested it on a server and it was a success!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.