Im trying to consume Wcf from Android , my code
private final static String SERVICE_URI = "http://188.59.2.211:8081/Survey.svc";
String email = "xx@gmail.com";
String password = "123";
public void onResume() {
super.onResume();
Login(email,password);
}
private void Login(String email, String password)
{
try {
URL url = new URL(SERVICE_URI + "/Login/email="+email+"&password="+password);
HttpGet request = new HttpGet(url.toURI());
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(request);
if ( response != null )
{
Log.i( "login", "received " + response.toString());
}
else
{
Log.i( "login", "got a null response" );
}
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and the wcf service side,
[ServiceContract(Namespace = "http://saas.com")]
public interface ISurvey
{
[OperationContract]
[WebGet(
UriTemplate = "/Login/email={email}&password={password}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
LoginResult Login(string email, string password);
}
and my config,
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="httpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="SAASService.Survey">
<endpoint address=""
behaviorConfiguration="httpBehavior"
binding="webHttpBinding"
contract="SAASService.ISurvey" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
The problem is that, Im using the below url,
“http://188.59.2.211:8081/Survey.svc/Login/email=xx@gmail.com&password=123”
and it is always giving “Bad Request” error , statusCode = 400
What is wrong?
Calling a web service like that from Android won’t work. Also, SOAP is xml, not json, so your content type is wrong.
To consume web services in Android, it’s best to use the ksoap-android library.
You can check this tutorial for sample code.
Here is a sample code that I used in one of my recent projects.