【发布时间】:2014-08-31 06:13:38
【问题描述】:
我正在创建一个与 SOAP Web 服务交互以从数据库中获取数据的应用程序。 当用户通过 web-services 传递用户名和密码成功登录时,会为该用户提供一个唯一的会话 ID。稍后在其他活动中将需要此会话 ID 来调用 Web 服务方法。我的问题是,如何在需要时将该会话 ID 传递给下一个活动并维护它直到用户注销。 例如: 这是一种网络服务方法,需要传递 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
}
【问题讨论】:
标签: java android web-services session android-ksoap2