6

I am trying to post data from Xamarin android app to Netsuite RESTlet api. Below code is working fine from asp.net web form c# using rest sharp library and also work on Postman Desktop app. But when i try to post data xamarin android app it throw below errors.

  • Response Error: name resolution error

and sometime it throw following error

request time out

                    var client = new RestClient("testclient.com");
                    var request = new RestRequest(Method.POST);
                    request.AddHeader("postman-token", "123-456-789-123-3654");
                    request.AddHeader("cache-control", "no-cache");
                    request.AddHeader("content-type", "application/json");
                    request.AddHeader("authorization", "NLAuth nlauth_account= 
                    12345678, nlauth_email=test@test.com, 
                    nlauth_signature=test123, nlauth_role=correctrole");
                    request.AddParameter(
                    "application/json", 
                    "[{ \"id\": \"123\", \t\"myid\": \"111\", \t\"qty\": \"4\" } , { \"id\": \"123\", \t\"myid\": \"618\", \t\"qty\": \"6\" } \n, { \"id\": \"123\", \t\"1234\": \"2037\", \t\"qty\": \"3\" } , { \"id\": \"123\", \t\"1243\": \"126\", \t\"qty\": \"2\" }]",
                    ParameterType.RequestBody);
                    IRestResponse response = client.Execute(request);
Salman
  • 333
  • 4
  • 18
  • Are you sure you have internet permission activated in your manifest. or if it's emulator it's have internet connectivity ? – Anirudha Gupta Jan 02 '18 at 10:27
  • 1
    Please don't build your JSON by hand like that. It's just asking for undetectable fiddly little syntax errors which won't become apparent until runtime. There's really no need when there are multiple (free) good quality JSON libraries available which can build the objects correctly, and even serialise directly from your business objects. JSON.NET is just one such example, there are others. – ADyson Jan 02 '18 at 10:31
  • @Ardian I have activated internet permission in manifest but still showing following error. **Error: NameResolutionFailure** – Salman Jan 02 '18 at 10:47
  • Can you access to testclient.com from android webbrowser? if you can't, it means that your are using a bad internet conection (for example: a wifi whose DNS can not resolve testclient.com ) – Jose M. Jan 05 '18 at 08:28
  • @Jose M. But how can i know that the issue is in internet connection? – Salman Jan 05 '18 at 10:19
  • @Jose M. All other test API 's giving correct response. and this testclient.com also give response in C# MVC web application – Salman Jan 05 '18 at 10:21

3 Answers3

2

There are several things you can try:

  1. Ensure all packages are up-to-date
  2. Use Fiddler to view network traffic and check the request going out and the response you are receiving. A tutorial can be found here
  3. Check that your request URL is complete (including the trailing '/' character for example), sometimes forgetting this causes a redirect and HttpClient sometimes has issues with that
  4. Replace RestSharp client with basic System.Net.HttpClient. This solution gives you less high-level approach, but you can see if the problem persists
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
1

I remember having to deal with this problem once and I think the solution is to use the IP address directly instead of the domain which leads to said IP address. Please give it a shot and let me know if it helps.

Raimo
  • 1,494
  • 1
  • 10
  • 19
0

I had the same problem in a local development network with the Android Emulator using Xamarin.Forms and RestSharp v106.6.9 in a Visual Studio 2019 project. At the beginning a simple call using "BaseUrl": "http://MachineName:63740" was working fine:

        var restResponse = restClient.Execute(restRequest);
        var content = restResponse.Content;

All of the sudden it no longer resolved the MachineName. Not even after I restarted the server machine where the API Web Services were running and also the consuming client machine. Changed the MachineName to its IP Address in the "BaseUrl": "http://192.168.0.2:63740" and everything started working again. So Raimo's answer here was the only way to solve my problem. This thread might point to the root cause of the problem: https://issuetracker.google.com/issues/36916949

heroesch
  • 150
  • 1
  • 1
  • 8