簡體   English   中英

Android中用於Web服務的ID會話管理

[英]ID session management in Android for web-services

我正在創建一個與SOAP Web服務交互以從數據庫獲取數據的應用程序。 當用戶通過Web服務傳遞用戶名和密碼成功登錄后,將為該用戶提供唯一的會話ID。 稍后在其他活動中將需要此會話ID來調用Web服務方法。 我的問題是,如何在需要時將該會話ID傳遞給下一個活動,並保持該狀態直到用戶注銷。 例如:這是一個Web服務方法,需要傳遞SID(會話ID),我如何獲取登錄時提供給我的會話ID,並將其用於此方法

-<element name="createAttachment">


-<complexType>


-<sequence>

<element name="sid" type="xsd:int"/>

<element name="repositoryHandle" type="xsd:string"/>

<element name="objectHandle" type="xsd:string"/>

<element name="description" type="xsd:string"/>

<element name="fileName" type="xsd:string"/>

</sequence>

</complexType>

</element>

那就是我的login.java。 我可以成功登錄並獲得會話ID

public class Login extends ActionBarActivity implements OnClickListener  {

    private static final String NAMESPACE = "***";
    private static final String URL = ***"; 
    private static final String METHOD_NAME = "login";
    private static final String SOAP_ACTION =  NAMESPACE + "/" + METHOD_NAME; //in wsdl it's nothing


    EditText usersusername, userspassword;
    Button LB;



    @Override 


    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login); 
         LB = (Button) findViewById(R.id.loginbutton);
         LB.setOnClickListener(this);
         usersusername = (EditText)findViewById(R.id.editusername);
         userspassword = (EditText)findViewById(R.id.editpassword);




    }

    public void onClick(View view){

    switch (view.getId()){

    case R.id.loginbutton:
    new LongOperation().execute("");

    break;


    }
    }


    private class LongOperation extends AsyncTask<String, Void, String>{ 
        @Override
        protected String doInBackground(String... params) {

           SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           usersusername = (EditText)findViewById(R.id.editusername);
           userspassword = (EditText)findViewById(R.id.editpassword);
           String user_Name = usersusername.getText().toString();
           String user_Password = userspassword.getText().toString();


           PropertyInfo unameProp =new PropertyInfo();
           unameProp.setName("userName");//Define the variable name in the web service method
           unameProp.setValue(user_Name);//set value for userName variable
           unameProp.setType(String.class);//Define the type of the variable
           request.addProperty("username",user_Name);//Pass properties to the variable

           PropertyInfo passwordProp =new PropertyInfo();
           passwordProp.setName("password");
           passwordProp.setValue(user_Password);
           passwordProp.setType(String.class);
           request.addProperty(passwordProp);

           SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // Declare the version of the soap request
           envelope.setOutputSoapObject(request);

           try {
                HttpTransportSE aht = new HttpTransportSE(URL);


                aht.call(SOAP_ACTION, envelope);  //this is the actual part that calls the web service
                SoapPrimitive result =(SoapPrimitive)envelope.getResponse();
                String Something = result.toString(); // Result string
                System.out.println(Something);
                if (!TextUtils.isEmpty(Something)) 
                {

                   Intent intent = new Intent(Login.this,Dashboard.class);
                   intent.putExtra("username",usersusername.getText().toString());
                   startActivity(intent);

           }
           }
           catch (Exception e){
            e.printStackTrace(); 
           }



            return null; 

        } // closing bracket of do in background
        @Override
                // show dialog and prepare the fields for retry 


            }
        }  

    } // closing bracket of long operation  
}

您可以將sessionID保存在共享首選項中,並在注銷時從共享首選項中刪除sessionID。 請參考此鏈接以獲取示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM