Android 通过kSoap将对象发送到webservice
我有一个登录视图,当用户输入用户名、密码时,我需要将它们发送到Web服务。 然后,服务将验证凭据并返回一个ID(customerid) 需要向服务发送“凭据”类的对象 班级:Android 通过kSoap将对象发送到webservice,android,soap,android-ksoap2,Android,Soap,Android Ksoap2,我有一个登录视图,当用户输入用户名、密码时,我需要将它们发送到Web服务。 然后,服务将验证凭据并返回一个ID(customerid) 需要向服务发送“凭据”类的对象 班级: public class Credetials { private String username; private String password; public Credetials(){} public String getUserName(){ return
public class Credetials {
private String username;
private String password;
public Credetials(){}
public String getUserName(){
return this.username;
}
public void setUserName(String uname){
this.username=uname;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.password=password;
}
}
视图/活动:
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/* reading username and password*/
String username=loginname.getText().toString();
String password=logincode.getText().toString();
Credetials credentials=new Credetials();
credentials.setUserName(username);
credentials.setPassword(password);
Log.d("username", credentials.getUserName());
Log.d("password", credentials.getPassword());
**// here i have proper username and password.**
new callGetCustomerId().execute(credentials);
}
});
class callGetCustomerId extends AsyncTask<Object, Void, String>{
@Override
protected String doInBackground(Object... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("credentials", params[0]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
return response.toString();
}catch(Exception e){
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
}
这是一项活动:
私有静态最终字符串SOAP_ACTION=”“;
私有静态最终字符串NAMESPACE=“mynamespace”;
私有静态最终字符串方法\u NAME=“getCustomerId”;
私有静态最终字符串URL=“myurl”;
异步任务
----------------
类callGetCustomerId扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(对象…参数){
SoapObject请求=新的SoapObject(名称空间、方法名称);
Credetials cred=新的Credetials(“用户名”、“密码”);
PropertyInfo credPropertyinfo=新的PropertyInfo();
credPropertyinfo.setName(“凭证”);
credPropertyinfo.setValue(cred);
credPropertyinfo.setType(cred.getClass());
request.addProperty(credPropertyinfo);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
addMapping(命名空间,Credetials.class.getSimpleName(),Credetials.class);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
androidHttpTransport.debug=true;
试一试{
//Log.d(“intortry”、“insidetry”);
调用(SOAP_操作,信封);
System.out.println(“aht请求转储为:”+androidHttpTransport.requestDump);
System.out.println(“aht responseDump是:“+androidHttpTransport.responseDump”);
SoapObject响应=(SoapObject)envelope.bodyIn;
//返回response.toString();
返回“呼叫成功”;
}捕获(例外e){
//返回e.getMessage();
返回e.toString();
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT.show();
//
}
}
WSDL
我已经在输出转储上面写过了 您必须序列化凭证类。尝试在该类中实现序列化。如果您愿意,您可以简单地执行类似“request.addProperty(“用户名”,您的用户);request.addProperty(“密码,您的通行证”);”这样的操作。根据WSDL契约,我尝试了公共类Credetials实现Serializable()…但没有成功。还是一样的错误,等等。现在你可以提出要求了。FaultString来自服务器,所以您可以进行连接。响应的类型取决于WS-contract和服务器。好的..那么我现在需要尝试什么呢??你想看WSDL吗?还是别的什么?我现在有点迷路了……没错。发布WSDL。“request.addProperty(,)”,方法名称,命名空间,soapAction。。。依赖WSDL契约
<xs:complexType name="getCustomerId">
<xs:sequence>
<xs:element name="credetials" type="tns:credentials" minOccurs="0"/>
</xs:sequence>
aht responseDump is :
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"> <faultcode>S:Server</faultcode>
<faultstring>java.lang.NullPointerException</faultstring>
<detail><ns2:exception xmlns:ns2="http://jax-ws.dev.java.net/" class="java.lang.NullPointerException"
note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false">
<ns2:stackTrace><ns2:frame class="presentation.ws.ElderlyWebServiceImpl" file="ElderlyWebServiceImpl.java" line="196" method="getNurseId"/>
<ns2:frame class="sun.reflect.GeneratedMethodAccessor1173" line="unknown" method="invoke"/><ns2:frame class="sun.reflect.DelegatingMethodAccessorImpl"
public class Credetials implements KvmSerializable{
/**
*
*/
private static final long serialVersionUID = 920795244030577363L;
/**
*
*/
private String username;
private String password;
public Credetials(){}
public Credetials(String username,String password){
this.username=username;
this.password=password;
}
public Object getProperty(int index) {
Object object = null;
switch (index)
{
case 0:
{
object = this.username;
break;
}
case 1:
{
object = this.password;
break;
}
}
return object;
}
public int getPropertyCount() {
// TODO Auto-generated method stub
return 2;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo propertyInfo) {
// TODO Auto-generated method stub
switch (index)
{
case 0:
{
propertyInfo.name = "username";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
}
case 1:
{
propertyInfo.name="password";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
}
}
}
public void setProperty(int index, Object obj) {
// TODO Auto-generated method stub
switch (index)
{
case 0:
{
this.username = obj.toString();
break;
}
case 1:
{
this.password = obj.toString();
break;
}
}
}
}
private static final String SOAP_ACTION = "";
private static final String NAMESPACE = "mynamespace";
private static final String METHOD_NAME = "getCustomerId";
private static final String URL = "myurl";
The asynctask
----------------
class callGetCustomerId extends AsyncTask<Object, Void, String>{
@Override
protected String doInBackground(Object... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
Credetials cred=new Credetials("username","password");
PropertyInfo credPropertyinfo=new PropertyInfo();
credPropertyinfo.setName("credentials");
credPropertyinfo.setValue(cred);
credPropertyinfo.setType(cred.getClass());
request.addProperty(credPropertyinfo);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, Credetials.class.getSimpleName(), Credetials.class);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug=true;
try{
//Log.d("intotry", "inside try");
androidHttpTransport.call(SOAP_ACTION, envelope);
System.out.println("aht requestDump is :"+androidHttpTransport.requestDump);
System.out.println("aht responseDump is :"+androidHttpTransport.responseDump);
SoapObject response = (SoapObject) envelope.bodyIn;
//return response.toString();
return "call success";
}catch(Exception e){
//return e.getMessage();
return e.toString();
}
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
//
}
}
<xs:complexType name="credentials">
<xs:sequence>
<xs:element name="password" type="xs:string" minOccurs="0"/>
<xs:element name="username" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getCustomerId">
<xs:sequence>
<xs:element name="credetials" type="tns:credentials" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getCustomerIdResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>