2012-03-08 37 views
0

我是新來的機器人請幫助我。
這是代碼,我已經嘗試了:如何通過在android中調用wcf web服務來驗證用戶名和密碼? &如何傳遞用戶名和密碼以及請求?

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

btnLogin=(Button)findViewById(R.id.btnLogin); 
btnCancel=(Button)findViewById(R.id.btnCancel); 

btnLogin.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 

    username=(EditText)findViewById(R.id.et_un); 
    password=(EditText)findViewById(R.id.et_pwd); 
    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);> 

如何通過請求參數?

PropertyInfo p1=new PropertyInfo() 
    p1.setName("username"); 
    p1.setValue("nitinr"); 
    request.addProperty(p1); 
    PropertyInfo p2=new PropertyInfo(); 
    p2.setName("password"); 
    p2.setValue("123"); 
    request.addProperty(p2); 

如何爲此編碼? 我不知道如何解析響應的輸出? ,並指導將會有什麼迴應?

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    envelope.dotNet = true;     
    envelope.setOutputSoapObject(request);        
    HttpTransportSE httpTransport = new HttpTransportSE(URL); 
    //invkoe web service and get result 
    try {      
httpTransport.call(SOAP_ACTION, envelope); 
SoapObject response = (SoapObject) envelope.getResponse(); 
tv.setText(response.toString()); 
    } catch (Exception exception) {        
tv.setText(exception.toString());          
    } 

if(username.equals("nitinr")){ 
if(password.equals("123")){ 
     onClick(v); 
} else{ 
lblResult.findViewById(R.id.tv_link); 
} 
}else{ 
    btnLogin.findViewById(R.id.tv_valid) 
} 
    } 

    }); 

} 
public void onClick(View v) { 
// TODO Auto-generated method stub 
Intent i=new Intent(getBaseContext(),Welcome.class); 
startActivity(i); 

    } 
} 

回答

1

如果您使用的HTTP請求方法使用這個鏈接... Httprequest

+0

嗨,我得到錯誤像'03 -08 02:35:50.046:E/AndroidRuntime(779):\t在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) – user1256259 2012-03-08 09:50:08

0

如果您正在使用SOAP Web服務,那麼你需要檢查你的服務,爲您呼叫的aservice行動的響應。

例如它可能返回1成功登錄或-1爲無效用戶或密碼,0用戶不存在。

/** 
* Login web services to authenticate the user. 
* @param Uname 
* @param Pword 
* @return 
*/ 
public static int loginCheck(String Uname, String Pword){ 
    int id= 0; 

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,SOAPLOGINMETHOD); 

    request.addProperty("username",Uname); 
    request.addProperty("password",Pword); 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 
    envelope.dotNet = true; 

    envelope.setOutputSoapObject(request); 
    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 


    try{ 
     httpTransport.call(SOAPLOGINACTION, envelope); 
     SoapObject response=(SoapObject) envelope.bodyIn; 
     Log.d("parameter", response.getProperty(0).toString()); 
     id=Integer.parseInt(response.getProperty(0).toString()); 
     } 
     catch (Exception exception){ 

      Log.d("Login Web Service Exception",exception.toString()); 
      exception.printStackTrace(); 
     } 


    return id; 
} 

使用回報:

public void onLoginClick(View view){ 
    userName=userNameTxt.getText().toString(); 
    password=passwordTxt.getText().toString(); 
    int uId=WebMethod.loginCheck(userName, password); 
    if(uId >0){ 
    // Do what you want with it 
    } 
+0

謝謝,這對我很有用。 – user1256259 2012-03-08 10:24:35

+0

你可以接受作爲答案:)謝謝 – jahan 2012-03-08 10:38:57

相關問題