我有一个登录视图,从视图中,当用户输入用户名、密码时,我需要将它们发送到网络服务。然后该服务将验证凭据并返回一个 ID.(customerid)。
该服务需要发送一个“凭据”类的对象。
班上:
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"/>
所以这就是我的回应......我需要在客户端做些什么吗?或者服务需要改变什么?
编辑: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>
我已经在输出转储上面写了。