通过kSoap将对象发送到webservice

时间:2013-01-23 08:48:30

标签: android soap android-ksoap2

我有一个登录视图,从视图中,当用户输入用户名,密码时,我需要将它们发送到webservice。 然后,该服务将验证凭据并返回ID。(customerid)。

该服务需要发送一个'Credential'类的对象。

班级:

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();

    }
}

问题:

现在问题是它总是进入catch块。 错误显示出像“无法序列化:blah blah..Credentials @ 413 ....” 任何人都可以帮助我解决我做错的事情。 ??

我发送了单个字符串参数,之前工作正常...所以我相信我发送'凭证'对象的方式可能是错误的。!!

编辑:

这是来自网络服务

<xs:complexType name="getCustomerId">
 <xs:sequence>
  <xs:element name="credetials" type="tns:credentials" minOccurs="0"/>
 </xs:sequence>

编辑2:

在尝试了很多事情后,我有以下响应转储。

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" 

file =“DelegatingMethodAccessorImpl.java”line =“25”method =“invoke”/&gt;                                                                                                                             

所以这就是我的反应......我在客户端需要做些什么? 或者服务需要改变什么?

编辑:3

这就是代码现在的样子..

班级

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();
        //

    }
}

WSDL

<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>

我在输出转储上面写了。

0 个答案:

没有答案