gpt4 book ai didi

java - Android 中用于 Web 服务的 ID session 管理

转载 作者:太空宇宙 更新时间:2023-11-04 14:43:48 24 4
gpt4 key购买 nike

我正在创建一个与 SOAP Web 服务交互以从数据库获取数据的应用程序。当用户通过 web-services 传递用户名和密码成功登录时,会为该用户提供一个唯一的 session ID。稍后在其他 Activity 中调用 Web 服务方法时将需要此 session ID。我的问题是,如何在需要时将该 session ID 传递给下一个 Activity 并维护它直到用户注销。例如:这是一个Web服务方法,需要传递SID( session ID),我如何获取登录时给我的 session 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。我可以成功登录并获得 session 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。请参阅此链接 Example

关于java - Android 中用于 Web 服务的 ID session 管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24666904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com