Base solution for your next web application
Open Closed

Android has a problem to communicate with the server, iOS is working fine #7579


User avatar
0
cictan created

I can run the iOS project on simulators and devices, however the android project has a problem.

How can I fix it?


9 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    What is your product version?

  • User Avatar
    0
    cictan created

    7.1

  • User Avatar
    0
    alper created
    Support Team

    Does it work when you use your local API? like localhost:5000 it may be related to your server with HTTP2 configuration just try it with your local environment and tell us the result.

  • User Avatar
    0
    cictan created

    It doesn't work when I use my local API, iOS project is working fine when using local API. Here is the error message:

    I can see the host is running through browser by accessing this page: http://127.0.0.1:5000/Ui/Login

  • User Avatar
    0
    alper created
    Support Team

    sorry my fault! the address 127.0.0.1 or localhost is not right. Try using your localhost real IP like 192.168.1.22....

  • User Avatar
    0
    cictan created

    Hi Alper, Yes, it's working now. I can run my android project on a simulator, can you tell me how fix the connection issue to my remote server? I'd like to use our production url/server url for the APIs. Thanks,

  • User Avatar
    0
    alper created
    Support Team

    it connects to your local development environment, so it means there's a problem with the PROTOCOL of the server. can you switch to HTTP to check if there's an issue with the SSL.

    Use ;

    private const string DefaultHostUrl = "http://dobany.com/";
    
  • User Avatar
    0
    alper created
    Support Team

    Other solution, probably better one... Use AndroidClientHandler class as your HttpMessageHandler for Android platform. You can use the implementation below;

    ModernHttpClientFactory.cs | Project: MyProject.Application.Client

    public class ModernHttpClientFactory : DefaultHttpClientFactory
        {
            /// <summary>
            /// Callback function for refresh token is expired
            /// </summary>
            public Func<Task> OnSessionTimeOut { get; set; }
    
            /// <summary>
            /// Callback function for access token refresh
            /// </summary>
            public Func<string, Task> OnAccessTokenRefresh { get; set; }
    
            public Func<HttpMessageHandler> CustomHandler { get; set; }
    
            public override HttpMessageHandler CreateMessageHandler()
            {
                HttpMessageHandler httpMessageHandler;
    
                if (CustomHandler != null)
                {
                    httpMessageHandler = CustomHandler();
                }
                else
                {
                    httpMessageHandler = new NativeMessageHandler
                    {
                        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                    };
                } 
    
                return new AuthenticationHttpHandler(httpMessageHandler)
                {
                    OnSessionTimeOut = OnSessionTimeOut,
                    OnAccessTokenRefresh = OnAccessTokenRefresh
                };
            }
        }
    

    SplashActivity.cs | Project: MyProject.Mobile.Droid

            private static void ConfigureFlurlHttp()
            {
                var modernHttpClientFactory = new ModernHttpClientFactory
                {
                    OnSessionTimeOut = App.OnSessionTimeout,
                    OnAccessTokenRefresh = App.OnAccessTokenRefresh,
                    CustomHandler = () => new Xamarin.Android.Net.AndroidClientHandler()
                };
    
                FlurlHttp.Configure(c =>
                {
                    c.HttpClientFactory = modernHttpClientFactory;
                });
            }
    
  • User Avatar
    0
    cictan created

    Hi Alper, Both worked finally! Thanks,