请问,如何正确让android客户端与WCF服务器端通信?
以下是WCF服务端(IP:192.168.61.177):
解决方案 Wcftest
1、IJsonServiceTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;namespace Wcftest
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IJsonServiceTest
{ [OperationContract(Name = "GetAccountDataJson")]
[WebGet(RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetAccountData", BodyStyle = WebMessageBodyStyle.Bare)]
List<Account> GetAccountData(); // TODO: 在此添加您的服务操作
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class Account
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public DateTime Birthday { get; set; }
} }
2:jsonService.scv.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;namespace Wcftest
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class JsonService : IJsonServiceTest
{
public List<Account> GetAccountData()
{a
return MockAccount.AccountList;
} }
public class MockAccount
{
public static List<Account> AccountList
{
get
{
var list = new List<Account>();
list.Add(new Account { Name = "Bill Gates", Address = "YouYi East Road", Age = 56, Birthday = DateTime.Now });
list.Add(new Account { Name = "Steve Paul Jobs", Address = "YouYi West Road", Age = 57, Birthday = DateTime.Now });
list.Add(new Account { Name = "John D. Rockefeller", Address = "YouYi North Road", Age = 65, Birthday = DateTime.Now });
return list;
}
}
} }3:web.Config
<?xml version="1.0" encoding ="utf-8"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" debug="true">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<services>
<service
name="WcfTest.JsonService" >
<host>
<baseAddresses>
<add baseAddress="http://192.168.61.177/TestServer/" />
</baseAddresses>
</host> <!-- This endpoint is exposed at the base address provided by the host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="webHttpBinding"
contract="WcfTest.IJsonServiceTest"
bindingConfiguration="webHttpBinding_IDataService"
name="JsonService"
>
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- the mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex
To expose the IMetadataExchange contract, you
must enable the serviceMetadata behavior as demonstrated below -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services> <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="">
<!-- The serviceMetadata behavior publishes metadata through
the IMetadataExchange contract. When this behavior is
present, you can expose this contract through an endpoint
as shown above. Setting httpGetEnabled to true publishes
the service's WSDL at the <baseaddress>?wsdl
eg. http://localhost/servicemodelsamples/service.svc?wsdl -->
<serviceMetadata httpGetUrl="mex" httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding_IDataService"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
<security mode="None" />
</binding> </webHttpBinding> </bindings> </system.serviceModel> <system.webServer>
<!--modules runAllManagedModulesForAllRequests="true" /-->
</system.webServer>
<connectionStrings>
<add name="CYDB2008Entities" connectionString="Data Source=.;Initial Catalog=CYDB2008;Integrated Security=True"/>
</connectionStrings>
</configuration>在android客户端调用:
public class WFCTools {
final static String SOAP_ACTION = "http://192.168.61.177/JsonService/GetAccountDataJson";
private static final String METHOD_NAME = "GetAccountDataJson";
private static final String NAMESPACE = "http://192.168.61.177/JsonService";
private static final String URL = "http://192.168.61.177/testserver/Jsonservice.svc";
public static String GetUser(){ return getJsonData_1();
}protected static String getJsonData_1(){
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("passonString", "Rajapandian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
return result.toString();
} catch (Exception e) {
return e.getMessage();
}
}返回错误:
expetced:END_TAB{http://schemas.xmlsoap.org/soap/envelope/}body(Postion:END_TAG<?{schemas.xmlsoap.org/soap/envelope/}s:Fault>@ ...java.io.InputStreamReader@)
以下是WCF服务端(IP:192.168.61.177):
解决方案 Wcftest
1、IJsonServiceTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;namespace Wcftest
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IJsonServiceTest
{ [OperationContract(Name = "GetAccountDataJson")]
[WebGet(RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetAccountData", BodyStyle = WebMessageBodyStyle.Bare)]
List<Account> GetAccountData(); // TODO: 在此添加您的服务操作
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class Account
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public DateTime Birthday { get; set; }
} }
2:jsonService.scv.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;namespace Wcftest
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class JsonService : IJsonServiceTest
{
public List<Account> GetAccountData()
{a
return MockAccount.AccountList;
} }
public class MockAccount
{
public static List<Account> AccountList
{
get
{
var list = new List<Account>();
list.Add(new Account { Name = "Bill Gates", Address = "YouYi East Road", Age = 56, Birthday = DateTime.Now });
list.Add(new Account { Name = "Steve Paul Jobs", Address = "YouYi West Road", Age = 57, Birthday = DateTime.Now });
list.Add(new Account { Name = "John D. Rockefeller", Address = "YouYi North Road", Age = 65, Birthday = DateTime.Now });
return list;
}
}
} }3:web.Config
<?xml version="1.0" encoding ="utf-8"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" debug="true">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<services>
<service
name="WcfTest.JsonService" >
<host>
<baseAddresses>
<add baseAddress="http://192.168.61.177/TestServer/" />
</baseAddresses>
</host> <!-- This endpoint is exposed at the base address provided by the host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="webHttpBinding"
contract="WcfTest.IJsonServiceTest"
bindingConfiguration="webHttpBinding_IDataService"
name="JsonService"
>
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- the mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex
To expose the IMetadataExchange contract, you
must enable the serviceMetadata behavior as demonstrated below -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services> <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="">
<!-- The serviceMetadata behavior publishes metadata through
the IMetadataExchange contract. When this behavior is
present, you can expose this contract through an endpoint
as shown above. Setting httpGetEnabled to true publishes
the service's WSDL at the <baseaddress>?wsdl
eg. http://localhost/servicemodelsamples/service.svc?wsdl -->
<serviceMetadata httpGetUrl="mex" httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding_IDataService"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
<security mode="None" />
</binding> </webHttpBinding> </bindings> </system.serviceModel> <system.webServer>
<!--modules runAllManagedModulesForAllRequests="true" /-->
</system.webServer>
<connectionStrings>
<add name="CYDB2008Entities" connectionString="Data Source=.;Initial Catalog=CYDB2008;Integrated Security=True"/>
</connectionStrings>
</configuration>在android客户端调用:
public class WFCTools {
final static String SOAP_ACTION = "http://192.168.61.177/JsonService/GetAccountDataJson";
private static final String METHOD_NAME = "GetAccountDataJson";
private static final String NAMESPACE = "http://192.168.61.177/JsonService";
private static final String URL = "http://192.168.61.177/testserver/Jsonservice.svc";
public static String GetUser(){ return getJsonData_1();
}protected static String getJsonData_1(){
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("passonString", "Rajapandian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
return result.toString();
} catch (Exception e) {
return e.getMessage();
}
}返回错误:
expetced:END_TAB{http://schemas.xmlsoap.org/soap/envelope/}body(Postion:END_TAG<?{schemas.xmlsoap.org/soap/envelope/}s:Fault>@ ...java.io.InputStreamReader@)
解决方案 »
- 如何更改GridView的item按下去的背景?
- android蓝牙通讯问题,调试不报错,直接运行报错???
- 请大家进来看看,十分重要,音频合并,多谢!!!
- 关于picker控件的问题
- 关于service启动的问题
- Android作真正的3D UI须要哪些知识点
- 一个android的初级学者 首先需要学些什么
- 在activity中怎么从values文件夹下的保存图片的xml中取图片
- 求一个Android调用.net写的webservice的例子
- android手机端与服务端通信
- android启动失败......打印launcher超时
- ArcGIS for Android SDK 1.1.1 用户界面Back后的问题
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;import com.android.wfc.R;import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class DoGetWFC extends Activity {
/** Called when the activity is first created. */
String s="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button doget=(Button) findViewById(R.id.hello);
doget.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
s=GetUser();
}
});
}
final static String SOAP_ACTION = "http://tempuri.org/IMyService/DoWorks";
private static final String METHOD_NAME = "DoWorks";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.100:8327/Near"; public String GetUser(){
new AlertDialog.Builder(this)
.setTitle("Android 提示")
.setMessage("这是一个提示,请确定"+getJsonData_1())
.show();
return getJsonData_1();
} protected static String getJsonData_1(){
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("passonString", "Rajapandian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
return result.toString();
} catch (Exception e) {
return e.getMessage();
}
}
}