0

I have an android application that does a GET Request to ASP.NET WEBAPI service i have created the service and it works when you open in browser while using II Express. However the application in my android device() is unable to reach the Service i created i'm using URL:http:/localhost:8306/api/uconnectservice and the LOGCAT recieved is as follows: java.net.SocketTimeoutException: failed to connect to /localhost(port 8306) after 10000ms. I have activated IIS7 and have tried looking over the NET.I don't know if it is server configuration issue.Pls any help or guidance needed thnx My Code For Android: String URL = "http://localhost:8306/api/uconnectservice";

    private final OkHttpClient client = new OkHttpClient();

    public void run() throws Exception {
        Request request = new Request.Builder().url(URL).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();

            }

            @SuppressLint("NewApi")
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try (ResponseBody responseBody = response.body()) {
                    if (!response.isSuccessful())
                        throw new IOException("Unexpected code " + response);

                    Headers responseHeaders = response.headers();
                    for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                        System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                    }

                    Log.e("Results", responseBody.string());
                }
            }
        });
    }

/////////////////////CODE FOR SERVER ASP.NET WEBAPI////////////////////
namespace UnicsPlcServices.Controllers
{
    public class UConnectServiceController : ApiController
    {
        private IUconnectRepository _UconnectHandler;

        public UConnectServiceController()
        {
            _UconnectHandler = new UconnectRepository();
        }

        public UConnectServiceController(IUconnectRepository repository)
        {
            if (_UconnectHandler == null)
            {
                throw new ArgumentNullException("repository");
            }
            _UconnectHandler = repository;
        }

        // GET api/uconnectservice
        public List<AcountTypesRegistered> Get()
        {
            return _UconnectHandler.GetAll();
        }
.........................other code`enter code here`
user3015410
  • 29
  • 1
  • 9
  • so you try to connect to `localhost:8306`, right? Are you sure that the server is running on the same device (on Android device)? – Vladyslav Matviienko Dec 13 '16 at 08:43
  • @Vlad WebApi application is running on IIS Express and i just took adress from there which is http://localhost:8306 and put in the request URL in android application. Is this wrong,what can i change for it to work – user3015410 Dec 13 '16 at 08:53
  • you need to replace `localhost` with IP address of the device, where your server is running. – Vladyslav Matviienko Dec 13 '16 at 09:04
  • @Vlad i did wht u said but still same message in LOGCAT: java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8306) after 10000ms. I'm using default IIS Express that comes with visual studio – user3015410 Dec 13 '16 at 09:15
  • is your device connected to the same network as the server? Are you sure that you use correct IP? – Vladyslav Matviienko Dec 13 '16 at 09:18

2 Answers2

-1

I had simmilar problem. I used android studio and visual studio with IIS express. My solution was to use classical IIS and in Android studio I used 10.0.2.2 instead of localhost.

IIS Express: You can use it, but you have to first allow external requests. (http://www.ryadel.com/en/iis-express-allow-external-requests-remote-clients-devices/)

Miroslav Adamec
  • 1,060
  • 1
  • 15
  • 23
  • i'm using Eclipse+visual studio+IIS express. I just used the address given to my service i.e. http://localhost:8306/ in the URL for android (see question). Is this wrong. I have activated IIS7 on my machine but don't know how to host pls – user3015410 Dec 13 '16 at 08:51
  • I made mistake. IP should be 10.0.2.2:8306. Try this first. If you dont need to debug your visual studio service, just copy content of Release (or Debug maybe) folder from your project directory to new IIS site directory. (creating site is simple - just select directory, name of site and port) – Miroslav Adamec Dec 13 '16 at 09:03
  • the error message i get in LOGCAT is : java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8306) after 10000ms..... – user3015410 Dec 13 '16 at 09:12
  • are you sure webservice is available on IIS? (make sure IIS Express is not running) – Miroslav Adamec Dec 13 '16 at 09:14
  • IIS Express has denied remote connections (android emulator). You have to enable it, if you want to use it. (for example http://stackoverflow.com/questions/3313616/iis-express-enable-external-request) – Miroslav Adamec Dec 13 '16 at 09:36
  • How to prevent IIS Express from running and use only IIS classic? – user3015410 Dec 13 '16 at 10:09
  • msigman provided very good answer here: http://stackoverflow.com/questions/9955696/how-do-i-start-stop-iisexpress-server – Miroslav Adamec Dec 13 '16 at 10:24
  • you can also attach visual studio to IIS (not IIS express) Project Properties -> Web -> Servers -> Change IIS Express to Local IIS -> Check Apply server settings to all users -> then Project URL to http://localhost/Example (your project) (http://stackoverflow.com/questions/38264772/using-iis-server-instead-of-iis-express-in-visual-studio-2015) – Miroslav Adamec Dec 13 '16 at 10:25
-1

Your first problem is that you're not using the IP address of the machine running the Web API, you need to use that in the Java code instead of localhost.

Your second problem is that IIS Express doesn't allow access from off localhost, but to get around that we made a free Visual Studio Extension called Conveyor, you can get it from the extensions dialog in VS or here https://visualstudiogallery.msdn.microsoft.com/a429dbb7-a982-4541-b401-934375c02c0f?redir=0

Without touching config files, it opens up IIS Express so you can connect to it from other machines, such as tablets and phones.

Jim W
  • 4,866
  • 1
  • 27
  • 43
  • @user3015410 I updated the answer to include information you already knew from other comments. – Jim W Jan 26 '17 at 18:19