1

I'm trying to call my web API endpoints from Android device connected via USB, but I get this error:

failed to connect to /192.168.1.156 (port 7259) from /192.168.1.159 (port 41916) after 10000ms

This is my API call:

LinearLayout loginButtonLL = (LinearLayout) findViewById(R.id.loginButtonLL);
    loginButtonLL.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            textViewLoginButtonText.setVisibility(View.GONE);

            ProgressBar progressBarLoginButton = findViewById(R.id.progressBarLoginButton);
            progressBarLoginButton.setVisibility(View.VISIBLE);

            OkHttpClient client = new OkHttpClient();
            String url="https://192.168.1.156:7259/api/Login/LoginUser";
            Request request = new Request.Builder()
                    .url(url)
                    .build();

            client.newCall(request).enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    call.cancel(); <-- ERROR IS HERE
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    final String myResponse = response.body().string();

                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //txtView.setText(myResponse);
                        }
                    });

                }
            });

        }
    });

The WebApi:

        using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;

    namespace DatezApi.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class LoginController : ControllerBase
        {
            [Route("LoginUser")]
            [HttpPost]
            public async Task<IActionResult> LoginUser()
            {
                return Ok(new { });
            }
        }
    }
1
  • Its almost certainly a firewall issue. If you're on a home network, your modem will be running a firewall and you'll need to poke a hole in it to access any server on another machine, even locally. If you're on a corporate network, make sure that the port is allowed for traffic. General rule is to test via the web browser to see if it gets a response. Commented Oct 27 at 15:35

1 Answer 1

0

1. Backend not reachable

Make sure that  the backend is listening on 192.168.1.156:7259.
sudo netstat -tuln | grep 7259

2. Firewall blocking

If  using Ubuntu or any firewall:
sudo ufw allow 7259/tcp | sudo ufw status

3. HTTPS with self-signed certificates

The API is running on HTTPS with a self-signed cert, which Android blocks by default.
For local testing, you can use:
String url = "http://192.168.1.156:7259/api/Login/LoginUser";

Or configure OkHttpClient to trust all certificates (for testing only):

OkHttpClient getUnsafeOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) {}
                public void checkServerTrusted(X509Certificate[] chain, String authType) {}
                public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
            }
        };

        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
        builder.hostnameVerifier((hostname, session) -> true);
        return builder.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

4. Android 9+ cleartext restriction

Since Android 9 (API 28+), HTTP traffic is blocked by default.
While using HTTP use this config to allow cleartext traffic:
res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.1.156</domain>
    </domain-config>
</network-security-config>

AndroidManifest.xml

<application
    android:networkSecurityConfig="@xml/network_security_config"
    android:usesCleartextTraffic="true"
    ... >
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.