Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is Complete Code service is live also
on 223.30.26.198/MyWcf

Service
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestfulService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)]
        [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml,
        Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetDateTime")] 

        
        string GetDateTime();     
    }


    
}









Service Defination
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestfulService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {

        #region IService1 Members

        public string GetDateTime()
        {
            return DateTime.Now.ToString();
        }

        #endregion
    }
}


Web Config
<system.webServer>
		<validation validateIntegratedModeConfiguration="false"/>
		<modules>
			<remove name="ScriptModule"/>
			<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
		</modules>
		<handlers>
			<remove name="WebServiceHandlerFactory-Integrated"/>
			<remove name="ScriptHandlerFactory"/>
			<remove name="ScriptHandlerFactoryAppServices"/>
			<remove name="ScriptResource"/>
			<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
		</handlers>
	</system.webServer>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v2.0.50727"><dependentAssembly>
				<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
				<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
			</dependentAssembly>
			<dependentAssembly>
				<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
				<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
			</dependentAssembly>
		</assemblyBinding></runtime>
	<system.serviceModel>
		<services>
			<service name="RestfulService.Service1" behaviorConfiguration="RestfulService.Service1Behavior">
				<!-- Service Endpoints -->
				<endpoint address="GetDateTime" binding="webHttpBinding" contract="RestfulService.IService1" behaviorConfiguration="Web" bindingConfiguration="">
					<!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
					<identity>
						<dns value="localhost"/>
					</identity>
				</endpoint>
				<host>
					<baseAddresses>
						<add baseAddress="http://223.30.26.198/mywcf"/>
					</baseAddresses>
				</host>
				<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
			</service>
		</services>
		<behaviors>
			<endpointBehaviors>
				<behavior name="Web">
					<webHttp/>
				</behavior>
			</endpointBehaviors>
			<serviceBehaviors>
				<behavior name="RestfulService.Service1Behavior">
					<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
					<serviceMetadata httpGetEnabled="true"/>
					<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
					<serviceDebug includeExceptionDetailInFaults="false"/>
				</behavior>
			</serviceBehaviors>
		</behaviors>
	</system.serviceModel>



Android Code
Java
package com.mkyong.android;


import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;


import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class AppActivity extends Activity {

	Button button;
	TextView tctVw;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		addListenerOnButton();
	}

	public void addListenerOnButton() {

		 

		button = (Button) findViewById(R.id.button1);
		tctVw=(TextView) findViewById(R.id.textView1);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				try
				{
				
					DefaultHttpClient httpClient = new DefaultHttpClient();
			        HttpGet request = new HttpGet("http://223.30.26.198/mywcf/Service1.svc/GetDateTime");
			        request.setHeader("Accept", "application/xml");
			        request.setHeader("Content-type", "application/xml");
			        HttpResponse response = httpClient.execute(request);
		        	HttpEntity entity = response.getEntity();
		        	String result = EntityUtils.toString(entity);
		        	tctVw.setText(result);
			        	
				}
				catch (Exception e)
				{
					e.printStackTrace();
					
                    					
				}

			}

		});

	}

}
Posted
Updated 20-Mar-14 2:11am
v3
Comments
Krunal Rohit 20-Mar-14 12:14pm    
Try [WebGet] in interface.

-KR
. _Puneet Sharma 21-Mar-14 0:57am    
Its Still not working showing no response while accessing in android Please provide me solution..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900