2012-04-03 41 views
0

我嘗試創建Web服務,爲Android appliaction下面的Web服務客戶端代碼:如何處理JAX-WS的用戶名和密碼的安卓要求

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

我的問題:我用淨豆JAX-WS服務器端, 如何我處理的用戶名的請求來自Android的WS -client &密碼,

IC完成只有request.addproperty(「用戶名」,用戶名);

PLZ,幫助..

回答

0

首先,在您的客戶端: UserNamePassWord都是字符串,但它們當作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; 
} 

在你的URL字符串的結尾刪除wsdl

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

在您的服務器:檢查出來http://www.ibm.com/developerworks/webservices/library/ws-android/index.html

希望它能幫助。

+0

謝謝,但我的問題不是在客戶端,而是在服務器端。我沒有收到參數值「用戶名」,服務器端的「密碼」,只有空值才能接收。簡而言之,我只在服務器端「C」發送一個對象,並且我在如何接收jax-ws服務器端的對象時感到困惑.i已經檢查了這個鏈接,但是此方法接收一個字符串參數而不是對象。我現在的事情你更好地理解我的問題。 – 2012-04-06 06:50:15

+0

好吧,我不熟悉jax-ws服務器端。希望有人能幫助ypu。 – 2012-04-06 11:17:21