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

标签 java android web-services session android-ksoap2

我正在创建一个与 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/

相关文章:

java - Reactor Core - Mono - onError Flatmap

java - 如何在 Spring MVC 中构建动态 URL?

rest - 我应该使用 POST 请求向我的服务器发送大量 ID 的检索请求吗?

java - @AroundInvoke 拦截器在@WebService 类上被调用两次

java - 当我在 Tomcat 上运行 Web 应用程序时,Tomcat 被停止

java - 将 RequestMapping 添加到 Controller

android - 实际边界上的涟漪效应

java - 一项或多项 Activity

android - TextView 中的图像可点击

java - Apache 耶拿 - java.lang.UnsupportedClassVersionError : com/hp/hpl/jena/rdf/model/ModelFactory