How to Consume WCf service in Android

  • Replies:16
  • OpenNot stickiedUnanswered
  • Forum posts: 8

Feb 27, 2014 11:24:42 AM via Website

Hi All,

I have created a simple WCF service and deploy it on internal server and trying to access it in my android application. Below is the code.

WCF: IService.cs
[XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml,
Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetUserForTest")]
string GetUserForTest();

Service.cs
public string GetUserForTest()
{
return "Result from Service";
}

Code for Android:
Button bSearchUser = (Button) findViewById(R.id.bSearchUser);
TextView firstName = (TextView) findViewById(R.id.txtFstName);
bSearchUser.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(User_Service_URL+"/GetUserForTest");
request.setHeader("Accept", "application/xml");
request.setHeader("Content-type", "application/xml");
try {
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
firstName.setText(result);

} catch (ClientProtocolException e) {
e.printStackTrace();

} catch (IOException e) {
e.printStackTrace();
}
}

});

The Client code[android code] returns: <html><body><p>File does not exist</p></body></html>

Please let me know what is going wrong in my code.

Thanks in Advance.

— modified on Feb 27, 2014 11:34:10 AM

Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 12:35:17 PM via Website

What returns the Browser, when you navigate to the URL?

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 8

Feb 28, 2014 1:10:33 PM via Website

Hi Pascal,

browser shows correct wsdl of that service. but from android app it returns "<html><body><p>File does not exist</p></body></html>".

Thanks,
Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 1:20:41 PM via Website

For the wsdl file you have to Change your URL to http://yourService/?wsdl
In your android Code you navigate to User_Service_URL+"/GetUserForTest, i think this is a spesific function in your Service but the URL has to be the same as in your browser
Edit: Normally, you use ksoap2 for consuming wcf in android

— modified on Feb 28, 2014 1:21:44 PM

LG Pascal //It's not a bug, it's a feature. :) ;)

Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 1:29:28 PM via Website

@chels yet: Sorry but i think you're in the false Forum here

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 8

Feb 28, 2014 2:04:11 PM via Website

Hi pascal,

I am not using ksoap2 for consuming wcf in android.

I am referring my server like User_Service_URL=yourService/?wsdl and yes u r correct "GetUserForTest" this is my method and i want to call this in android app.

Thanks,
Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 2:11:34 PM via Website

ok, I understand.
Then you have to cut away the wsdl statement with the "?" and add "/GetUserForTest"
so the url looks like: yourService/GetUserForTest (pattern: service/function)
Its because the wsdl file describes your service and .net can't find a file with the name: yourService/?wsdl/GetUserForTest"

LG Pascal //It's not a bug, it's a feature. :) ;)

Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 2:33:15 PM via Website

What kind of hosting method do you use? (SelfHosting, IIS hosting etc.) please Poste hosting code
Is the service local or in the internet?

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 8

Feb 28, 2014 2:41:43 PM via Website

Hi Pascal,

I am using IIS hosting and it is hosted on my company internal server.

Thanks
Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 2:44:22 PM via Website

Is the android in the same Network (with wlan)
Otherwise ist understandable this not working

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 8

Feb 28, 2014 2:48:16 PM via Website

No, service is on other network I am connecting it via VPN on my local machine.. generally we do that.

Thanks
Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Feb 28, 2014 2:52:14 PM via Website

What does android say when you open the URL in ist browser?
Do you have an IT Office in your Company?
May you can ask them because the indirect support over the Forum is a little bit difficult.

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 8

Mar 4, 2014 2:42:50 PM via Website

Hi Pascal,

When access the service from browser it is showing me correct data.



Thanks
Pascal P.
  • Mod
  • Blogger
  • Forum posts: 9,089

Mar 4, 2014 9:36:16 PM via Website

Then the answer is very easy:
1HttpGet request = new HttpGet('"testmercola.com/service.svc/MyHttpGetData");
2//...
But i ment when you open the URL in the android browser, then you can siee if ist is a Networking Problem or a app problem

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 8

Mar 5, 2014 9:41:32 AM via Website

Hi Pascal,

You were right. it was Networking Problem.

I am able to call WCF service in android project.

please let me know how to convert XML(output from service). Now it is showing correct output, but the result comes in xml format and I want a simple text format. let me know if I need to read it using xml classes or any other classes can do same thing.



Thanks