简体   繁体   中英

Connection to localhost refused in Android

I am trying to consume WCF service in Android. I have hosted the service as

http://localhost:8080/UserService.svc

This is my serviceModel section in web.config web.config :

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SimpleBasic">
          <security mode="None"/>
        </binding>
        <binding name="BasicOverHttps">
          <security mode="Transport"/>
        </binding>
      </basicHttpBinding>
    </bindings>
        <services>
            <service behaviorConfiguration="AndroidService.Service1Behavior" name="AndroidService.Service1">
                <endpoint address="" binding="wsHttpBinding" contract="AndroidService.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
            <service behaviorConfiguration="AndroidService.UserServiceBehavior" name="AndroidService.UserService">
                <endpoint address="ws" binding="wsHttpBinding" contract="AndroidService.IUserService">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
        <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="SimpleBasic" contract="AndroidService.IUserService"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="web" binding="webHttpBinding" contract="AndroidService.IUserService" />
      </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="AndroidService.Service1Behavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
                <behavior name="AndroidService.UserServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

which I can browse. I also checked the output for one method GetUser of service in WebServiceStudio. It came out to be fine. I then created an Android client application wherein I am trying to consume it.

HttpClient httpClient = new DefaultHttpClient();
          try
          {
              String url = "http://localhost:8080/UserService.svc/GetUser?name=Nitish";

            HttpGet method = new HttpGet( new URI(url) );
            HttpResponse response = httpClient.execute(method);
            if ( response != null )
            {
              Log.i( "login", "received " + getResponse(response.getEntity()) );
            }
            else
            {
              Log.i( "login", "got a null response" );
            }
          } catch (IOException e) {
            Log.e( "error", e.getMessage() );
          } catch (URISyntaxException e) {
            Log.e( "error", e.getMessage() );
          }
          catch (Exception e) {
            // TODO: handle exception
              Log.e( "error", e.getMessage() );
        }  

I get exception as follows in HttpResponse response = httpClient.execute(method);

Connection to http://localhost:8080 refused 

I am running the application on device and have added following premissions in mainefest:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  

I went through this solution and tried to host the service on my IP address but no success.

Localhost generally works only when the service and the client sit in the same host.

Use host machine name in the client request, like :

http://MyHostMachine:8080/...

If you are not using IIS but a Windows service to host the WCF service, in the app config, make sure the base address of respective endpoints is

http://MyHostMachine:8080/

"localhost" referes to the local device, and not your WCF-Service-host.

On your development-PC it is 127.0.0.1, which referes to this machine.

On your android-device it is the same, and this referes to the device.

Read this concerning "Localhost"

You have to replace "localhost" with the IP-address of your Machine on which the WCF-Service is running.

When you indicate localhost in an IP address that is translate into 127.0.0.1 so you can't refer to localhost into app's code because it tries to connect to itself to receive data. The correct solution IMHO is to replace "localhost" with the machine's IP address in the LAN. If you have Windows try to run a terminal and type "ipconfig" to discover the correct IP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM