简体   繁体   中英

WCF service Android connection

I am so new about android and wcf service. I have an expection.

This is android function:

private void Post(String VAL)
{
    HttpClient httpClient = new DefaultHttpClient();
    try
    {
        String aaa = "aaa";
        String url = "http://192.168.30.117:59151/IService1";
        HttpPost method = new HttpPost(url);
        StringEntity e = new StringEntity("{ \"something\":12345 }", "UTF-8");
        method.setEntity(e);
        method.setHeader("content-type", "application/json");
        method.setHeader("Accept", "application/json");
        HttpResponse response = httpClient.execute(method);
        HttpEntity entity = response.getEntity();
        if(entity != null)
        {
            System.out.println(entity);
        }
    } catch (IOException e) {
        Log.e( "error", e.getMessage() );
    }}

here is wcf part:

[ServiceContract()]
public interface IService1
{   

    [OperationContract]
    [WebInvoke(Method= "POST", UriTemplate = "GetData?parameter={parameter}", ResponseFormat = WebMessageFormat.Json)]
    string GetData(string parameter);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}


    public string GetData(String value)
    {
        return string.Format("You entered: {0}", value);
    }

and this is config file:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="http_behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="http_behavior" name="WcfServiceGps.Service1">

        <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
          name="GetData" contract="WcfServiceGps.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" name="GetDataMex"
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.30.117:59151/Service1.svc/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

when ı am calling the service from android

HttpResponse response = httpClient.execute(method);

it gives the following exception:

If you are using a virtual machine to compile your app on android, you must have visibility on the server, the same when you are using a virtual machine. You are getting that exception because your devices are not in the same network and also it's seem you are not deploying your server, you are just compiling it on Visual studio... what is it ? '59151' the port number of IIS express?.

Donwload IIS on windows and deploy your server, then use ipconfig command to know your ip address in your local network often of try to connect with your server using the android browser (writing your ip 192.168.XX). If it's works, try to call a REST service in your android browser, and if it's work as well you can now test your REST call in android.

By the way use Retrofit 2 as REST library.

If you call api in your main thread dont do it. Use AsyncTask to consume api. And put these line in your activity or fragment what you use.

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

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