如何在android上使用ksoap2调用WCF服务?
这是我的密码如何在android上使用ksoap2调用WCF服务?,android,wcf,ksoap2,Android,Wcf,Ksoap2,这是我的密码 import org.ksoap2.*; import org.ksoap2.serialization.*; import org.ksoap2.transport.*; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ksop2test extends Activity { /** Called when the acti
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ksop2test extends Activity {
/** Called when the activity is first created. */
private static final String METHOD_NAME = "SayHello";
// private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org";
// private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc";
// private static final String URL = "http://192.168.0.2:8080/webservice1/Service1.asmx";
final String SOAP_ACTION = "http://tempuri.org/IService1/SayHello";
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
TextView tv;
StringBuilder sb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
sb = new StringBuilder();
call();
tv.setText(sb.toString());
setContentView(tv);
}
public void call() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", "Qing");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
sb.append(envelope.toString() + "\n");//cannot get the xml request send
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
//to get the data
String resultData = result.toString();
// 0 is the first object of data
sb.append(resultData + "\n");
} catch (Exception e) {
sb.append("Error:\n" + e.getMessage() + "\n");
}
}
}
package cn.qing.ksop2test;
import java.io.Writer;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.widget.TextView;
public class ksop2test extends Activity {
/** Called when the activity is first created. */
private static final String METHOD_NAME = "HelloWorldRequest";
// private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
// private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc";
// private static final String URL = "http://192.168.0.2:8080/webservice1 /Service1.asmx";
final String SOAP_ACTION = "http://tempuri.org/IService1/HelloWorld";
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
TextView tv;
StringBuilder sb;
private XmlSerializer writer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
sb = new StringBuilder();
call();
tv.setText(sb.toString());
setContentView(tv);
}
public void call() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Name", "Qing");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
//to get the data
String resultData = result.toString();
// 0 is the first object of data
sb.append(resultData + "\n");
} catch (Exception e) {
sb.append("Error:\n" + e.getMessage() + "\n");
}
}
}
我可以成功访问.asmx服务,但当我尝试调用wcf服务时
虚拟机说:
错误:
应为:END_标记{}体(位置:END_标记http://schemas.xmlsoap.org/soap/envelope/}s:Fault>@1:712在java.io中。InputStreamReader@43ba6798
如何打印请求发送的内容
以下是wcf wsdl:
它在标记中使用
asmx在标签
中使用
有什么区别?在“理论”中,具有基本http绑定的wcf和asmx应该工作相同
这可能与WCF服务的配置方式有关
如果在客户端配置传输模式并在服务器上配置缓冲区,我们会遇到类似的问题。尽管不确定这是否与您的情况相关。最后我让它工作起来了
因为名称空间最后缺少“/”
下面是我的代码
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ksop2test extends Activity {
/** Called when the activity is first created. */
private static final String METHOD_NAME = "SayHello";
// private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org";
// private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc";
// private static final String URL = "http://192.168.0.2:8080/webservice1/Service1.asmx";
final String SOAP_ACTION = "http://tempuri.org/IService1/SayHello";
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
TextView tv;
StringBuilder sb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
sb = new StringBuilder();
call();
tv.setText(sb.toString());
setContentView(tv);
}
public void call() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", "Qing");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
sb.append(envelope.toString() + "\n");//cannot get the xml request send
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
//to get the data
String resultData = result.toString();
// 0 is the first object of data
sb.append(resultData + "\n");
} catch (Exception e) {
sb.append("Error:\n" + e.getMessage() + "\n");
}
}
}
package cn.qing.ksop2test;
import java.io.Writer;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.widget.TextView;
public class ksop2test extends Activity {
/** Called when the activity is first created. */
private static final String METHOD_NAME = "HelloWorldRequest";
// private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
// private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://192.168.0.2:8080/HelloWCF/Service1.svc";
// private static final String URL = "http://192.168.0.2:8080/webservice1 /Service1.asmx";
final String SOAP_ACTION = "http://tempuri.org/IService1/HelloWorld";
// final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
TextView tv;
StringBuilder sb;
private XmlSerializer writer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
sb = new StringBuilder();
call();
tv.setText(sb.toString());
setContentView(tv);
}
public void call() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Name", "Qing");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
//to get the data
String resultData = result.toString();
// 0 is the first object of data
sb.append(resultData + "\n");
} catch (Exception e) {
sb.append("Error:\n" + e.getMessage() + "\n");
}
}
}
我正在使用
private static final String SOAP_ACTION = "http://tempuri.org/IContact/GetContactCount";
private static final String METHOD_NAME = "GetContactCount";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://xxx.xxx.com/Contacts/ContactsService.Contacts.svc";
因此,问题可能在于您的SOAP操作
您的方法名拼写正确吗?ie AuthenticatdUser?谢谢您的回答。呼叫WCF服务真的很有帮助 在将outputSoapObject设置到信封中之后,我想为从web服务获得简单和复杂的输出添加此更正,如果我错了,请纠正我
envelope.setOutputSoapObject(requestSoapObject);
// if its dotnet web service then make it true
if (isDotNetWebService)
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(NAMESPACE + methodName, envelope);
if (useBodyIn) // use bodyIn if service method returns string/int
// etc
{
/* Gives output from webservice */
responseSoapObject = (SoapObject) envelope.bodyIn;
} else // use getResponse() if service method returns objects or
// array
{
/* Gives output from webservice */
responseSoapObject = (SoapObject) envelope.getResponse();
}
您可能无法使用ksoap,因为davalik JVM与Sun JVM不同。您可能需要编写自己的SOAP解析器。SOAP对于移动设备来说实在太重了,IMO。但我可以成功地从.asmx服务获取数据这是在android应用程序中访问WCF Web服务的非常好的演示代码………,我已经应用了此源代码和rE解决我的问题…..对我来说,根据方法返回的内容,有不同的响应类型。如果有一个返回值,则使用SoapPrimitive类型,如果有更复杂的类型,则使用SoapObject。你不知道这对我有多大帮助。谢谢堆。我也错过了“/”字符。:D