自己定义了webservice,有以下接口:getalluser (无参数),getuserbyname(有一个string类型名为arg0参数)在用wsCaller测试能正常运行,但是在android客户端只能正常调用getalluser方法,调用getuserbyname传递的参数在webservice控制台输出时null。
下面是android代码:
public class MainActivity extends Activity implements Serializable {
EditText name, pwd; // 用户名,密码框
Button login; // 登录按钮 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) this.findViewById(R.id.txtName);
pwd = (EditText) this.findViewById(R.id.txtPwd);
login = (Button) this.findViewById(R.id.btnLogin);
login.setOnClickListener(new LoginListener()); // 为登录按钮增加监听 } private static final long serialVersionUID = 1L;
// WSDL文档中的命名空间
private static final String targetNameSpace = "http://service.book.com/";
// WSDL文档中的URL
private static final String WSDL = "http://10.0.2.2:80/BookService/IUserImplPort";
// 需要调用的方法名,根据用户名获取用户
private static final String getUserByName = "getUserByName"; /**
 * @param txtName
 * @param txtPwd
 * @return 登陆结果 登陆
 */ public boolean login(String txtName, String txtPwd) {
//List<User> users = new ArrayList<User>();
System.out.println("1:" + "名字" + txtName + "密码" + txtPwd);
boolean result = false;
SoapObject request = new SoapObject(targetNameSpace, getUserByName);
request.addProperty("arg0", txtName);
System.out.println("2:" + request.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);//envelope.bodyOut = request
System.out.println("3:" + "envelope" + envelope.bodyOut.toString());
System.out.println("2.2:" + request.toString());
HttpTransportSE ht = new HttpTransportSE(WSDL);
System.out.println("---------------"+envelope.toString());
System.out.println("4:" + targetNameSpace);
System.out.println("5:" + getUserByName);
try { // envelope.dotNet = true;
// envelope.setOutputSoapObject(request);
envelope.encodingStyle = "UTF-8";
ht.call(targetNameSpace+getUserByName,envelope);
SoapObject results = (SoapObject) envelope.getResponse();
ht.debug = true;
int count = results.getPropertyCount();
System.out.println("6:" + count);
// System.out.println(results.getProperty(1));
             
if (results != null) {
// SoapObject soapObject = (SoapObject) envelope.getResponse();
SoapPrimitive Primitive = (SoapPrimitive) results
.getProperty("Passwords");
// SoapPrimitive Primitivew = (SoapPrimitive)
// results.getProperty("arg1");
System.out.println(Primitive);
// System.out.println(Primitivew);
if (Primitive != null) {
if (results.getName() == "anyType") {
// 获取单条的数据
String PassWords = Primitive.toString();
if (PassWords.equals(txtPwd)) {
result = true;
// 打印登陆信息,还是习惯用sysout,用不惯log
System.out.println("用户" + txtName + "登录成功");
} else {
System.out.println("用户" + txtName + "登录失败,密码错误");
}
}
} else {
System.out.println("用户" + txtName + "不存在");
}
}
} catch (XmlPullParserException e) {
//System.out.println(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return result;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} class LoginListener implements OnClickListener {
@Override
public void onClick(View v) {
String usernameStr = name.getText().toString().trim();
System.out.print(usernameStr);
String passwordStr = pwd.getText().toString();
boolean myresult = login(usernameStr, passwordStr); System.out.println("myresult:" + myresult);
Intent intent = new Intent();
if (myresult) {
intent.setClass(MainActivity.this, testActivity.class);
MainActivity.this.startActivity(intent);
}
} }}android输出日志为:05-07 16:29:39.355: I/System.out(1059): zhangsan1:名字zhangsan密码123456
05-07 16:29:39.355: I/System.out(1059): 2:getUserByName{arg0=zhangsan; }
05-07 16:29:39.536: I/System.out(1059): 3:envelopegetUserByName{arg0=zhangsan; }
05-07 16:29:39.536: I/System.out(1059): 2.2:getUserByName{arg0=zhangsan; }
05-07 16:29:39.588: I/System.out(1059): ---------------org.ksoap2.serialization.SoapSerializationEnvelope@405361e0
05-07 16:29:39.588: I/System.out(1059): 4:http://service.book.com/
05-07 16:29:39.596: I/System.out(1059): 5:getUserByName
05-07 16:29:40.035: I/System.out(1059): 6:1
05-07 16:29:40.035: I/System.out(1059): illegal property: Passwords
05-07 16:29:40.046: W/System.err(1059): java.lang.RuntimeException: illegal property: Passwords
05-07 16:29:40.046: W/System.err(1059):  at org.ksoap2.serialization.SoapObject.getProperty(SoapObject.java:179)
05-07 16:29:40.056: W/System.err(1059):  at com.example.myclient.MainActivity.login(MainActivity.java:84)
05-07 16:29:40.056: W/System.err(1059):  at com.example.myclient.MainActivity$LoginListener.onClick(MainActivity.java:156)
05-07 16:29:40.056: W/System.err(1059):  at android.view.View.performClick(View.java:2485)
05-07 16:29:40.056: W/System.err(1059):  at android.view.View$PerformClick.run(View.java:9080)
05-07 16:29:40.056: W/System.err(1059):  at android.os.Handler.handleCallback(Handler.java:587)
05-07 16:29:40.056: W/System.err(1059):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 16:29:40.096: W/System.err(1059):  at android.os.Looper.loop(Looper.java:123)
05-07 16:29:40.096: W/System.err(1059):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-07 16:29:40.096: W/System.err(1059):  at java.lang.reflect.Method.invokeNative(Native Method)
05-07 16:29:40.096: W/System.err(1059):  at java.lang.reflect.Method.invoke(Method.java:507)
05-07 16:29:40.096: W/System.err(1059):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-07 16:29:40.096: W/System.err(1059):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-07 16:29:40.105: W/System.err(1059):  at dalvik.system.NativeStart.main(Native Method)
05-07 16:29:40.105: I/System.out(1059): myresult:false
05-07 16:29:40.996: D/dalvikvm(351): GC_EXPLICIT freed 8K, 55% free 2591K/5703K, external 1625K/2137K, paused 128ms
05-07 16:29:41.646: D/SntpClient(132): request time failed: java.net.SocketException: Address family not supported by protocol
05-07 16:29:46.181: D/dalvikvm(461): GC_EXPLICIT freed <1K, 55% free 2534K/5511K, external 1625K/2137K, paused 216ms
05-07 16:34:41.706: D/SntpClient(132): request time failed: java.net.SocketException: Address family not supported by protocol
05-07 16:39:41.737: D/SntpClient(132): request time failed: java.net.SocketException: Address family not supported by protocol
求大神指导AndroidWeb服务