1

I have a rudimentary java android app written using eclipse. I am testing it on a device rather than the emulator (too slow). The MainActivity is currently only accessing a WCF url(code below). The WCF service is a simple GET method which is hosted on IIS. I can access the service method from firefox with the url given below. I have turned off the firewall for now. and on the command netstat -an I can see :

TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING

My problem is that though the service is accessible from the browser, I am unable to connect from the app. It keeps timing out.

I have tried localhost, 10.0.2.2 andd my lan ip as well.

My code is:

MainActivity

URL url = new URL("http://10.0.2.2:8080/tst/Service1.svc/getMsg/kkk");
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);   
    urlConnection.setRequestMethod("GET");  
    urlConnection.setUseCaches(false);  
    urlConnection.setConnectTimeout(10000);  
    urlConnection.setReadTimeout(10000);  
    urlConnection.setRequestProperty("Content-Type","application/json");   

    urlConnection.setRequestProperty("Host", "10.0.2.2:8080");
    urlConnection.connect();  
     int HttpResult =urlConnection.getResponseCode();  
    if(HttpResult ==HttpURLConnection.HTTP_OK)
    {  
    ....

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.secondprj"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="23" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>

</manifest>

Service code is

public class Service1 : IService1
{
    public string GetMessage(String name)
    {
        return "abc " + name;
    }
    ...

I have deployed it under folder 'tst' in IIS.

The Web.config is:

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
 <services>
  <service
      name="WcfService1.Service1"
     >
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080/"/>
      </baseAddresses>
    </host>
    <endpoint address=""
              binding="webHttpBinding"
              contract="WcfService1.IService1" 
              behaviorConfiguration="web"/>

  </service>
 </services>
 <behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceMetadata httpGetEnabled="false"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
 <endpointBehaviors>
  <behavior name="web" >
    <webHttp/>
  </behavior>
 </endpointBehaviors> 
 </behaviors>
 <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
 </protocolMapping>    
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"  multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>

<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>

Interface

namespace WcfService1
{
[ServiceContract]

public interface IService1
{
    [OperationContract]
   //[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
   // ResponseFormat = WebMessageFormat.Json,
   //UriTemplate = "getMsg")]
    [WebInvoke(Method = "GET",   
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "getMsg/{name}")]
    string GetMessage(string name);
}
}

Please look in to the code and let me know if I have made some mistake. Later I want to use POST rather than the GET

EDIT::: I have debugged the wcf using a call from the browser. There seems to be no problem in the debugging. I want to debug when the wcf is installed on IIs. How would i do that?? Also the call to the WCF comes from the android app. When the call comes fiddler did not show any new item. If any one has used this tool for intercepting requests from an android app please help...

EDIT2:: Tried changing the port of the wcf service. Now getting a 405 response. Please give your suggestions!

kavita
  • 845
  • 4
  • 14
  • 40
  • any more info required?? – kavita Aug 24 '15 at 07:35
  • I can access my service using my lan ip as well from the browser "http://192.168.10.102:8080/tst/Service1.svc/getMsg/jjj" – kavita Aug 24 '15 at 08:07
  • any answers?? please? – kavita Aug 24 '15 at 12:59
  • The easiest way to identify the problem is to use Fiddler to inspect your RAW request from Android app vs browser request to your WCF service. – Rajesh Aug 24 '15 at 15:23
  • will try this out and post the results here – kavita Aug 25 '15 at 05:45
  • Downloaded installed configured fiddler. Could debug wcf when creating a new request from fiddler. But couldnt get a request in fiddler when the call comes from the android device. Any pointers to how I can get the request from android in fiddler? – kavita Aug 25 '15 at 06:11
  • Can you change your internal IP to be localhost in your android code and then see if you can see the request in fiddler – Rajesh Aug 25 '15 at 08:23
  • How will I call from device then? Wouldnt i have to call using the emulator? – kavita Aug 25 '15 at 09:26
  • See if this helps you to capture incoming requests on your WCF machine : http://stackoverflow.com/questions/4428680/how-do-i-monitor-all-incoming-http-requests – Rajesh Aug 25 '15 at 09:34

0 Answers0