0

我尝试创建web服务,android appliaction的webservice客户端代码如下:

String SOAP_ACTION = "";
String METHOD_NAME = "operation1";
String NAMESPACE = "http://service.livebackup.com/";
String URL = "http://192.168.1.3:8084/test/webService?wsdl";

Button signin;
Thread t;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    signin = (Button) findViewById(R.id.btn_sign_in);
    signin.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
                showDialog(0);
                t=new Thread() {
                    public void run() {
                            tryLogin();
                      }

                };
          t.start();
          }
    });

}

private void tryLogin() {
    // TODO Auto-generated method stub
    EditText etxt_user = (EditText) findViewById(R.id.txt_username);
    EditText etxt_pass = (EditText) findViewById(R.id.txt_password);
    String username = etxt_user.getText().toString();
    String password = etxt_pass.getText().toString();

    callWebService(username,password);
}

private void callWebService(String username, String password) {
    // TODO Auto-generated method stub

    //String result = "ishan";
     try {
         int no=2;
         SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
      AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

       PropertyInfo p1 = new PropertyInfo();
       Check C = new Check();//use check KvmSerializable{
       C.UserName=username;
       C.PassWord=password;


       p1.setName("C");
       p1.setValue(C);
       p1.setType(C.getClass());
       request.addProperty(p1);
               SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
           envelope.dotNet=false;
                       envelope.setOutputSoapObject(request);
                   androidHttpTransport.call(SOAP_ACTION, envelope);
                   Log.v("Ok","wait for response");
                   Object results =(Object)envelope.getResponse();


                   Log.v("Ok","Connection has response object" +results);

                   Log.v("Ok","jj");
                   //to get the data String resultData=result.getProperty(0).toString();
                   String temp = results.toString();

                   Log.v("Ok","Connection has response" +temp);
     }
     catch (Exception e) {
        // TODO: handle exception
            System.out.println("Error:" +e.getClass().getName()+":"+e.getMessage());

    }

}

检查类如下:

public class Check implements KvmSerializable{

 public String UserName;
 public String PassWord;

 public Check(){}

 public Check(String userName, String passWord) {
        super();
        UserName = userName;
        PassWord = passWord;
    }

 @Override
public Object getProperty(int arg0) {
    // TODO Auto-generated method stub

     switch(arg0)
     {
     case 0:
         return UserName;
     case 1:
         return PassWord;
     }

    return null;
}

@Override
public int getPropertyCount() {
    // TODO Auto-generated method stub
    return 2;
}



@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    // TODO Auto-generated method stub

     switch(index)
     {
     case 0:
         info.type = PropertyInfo.INTEGER_CLASS;
         info.name = "UserName";
         break;
     case 1:
         info.type = PropertyInfo.INTEGER_CLASS;
         info.name = "PassWord";
         break;

     default:break;
     }


}

@Override
public void setProperty(int index, Object value) {
    // TODO Auto-generated method stub

    switch(index)
    {
    case 0:
        UserName = value.toString();
        break;
    case 1:
        PassWord = value.toString();
        break;
    default:
        break;
    }
}
}

我的问题:我在服务器端使用 net beans jax-ws,如何处理来自 android ws -client 的用户名和密码请求,

我只完成了 request.addproperty("userName",username);

请帮忙..

4

1 回答 1

0

首先,在您的客户端中: UserNameandPassWord是字符串,但您将它们视为 int。在Check课堂上这样做:

@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch(index) {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "UserName";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "PassWord";
            break;
        default:
            break;
}

wsdl在 URL 字符串的末尾删除:

String URL = "http://192.168.1.3:8084/test/webService/";

在您的服务器中:查看http://www.ibm.com/developerworks/webservices/library/ws-android/index.html

希望能帮助到你。

于 2012-04-05T18:00:38.157 回答