Java 在一个会话中连接android应用程序

Java 在一个会话中连接android应用程序,java,android,eclipse,soap,Java,Android,Eclipse,Soap,我正在制作这个android应用程序,用户必须登录才能进入该应用程序,我正在通过web服务将我的应用程序连接到数据库 我成功创建了登录页面,但是,我的问题是,用户如何使用相同的会话ID进入第二个页面,系统如何检索登录其他页面的用户详细信息 这是我的login.java代码 import android.R.string; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar

我正在制作这个android应用程序,用户必须登录才能进入该应用程序,我正在通过web服务将我的应用程序连接到数据库 我成功创建了登录页面,但是,我的问题是,用户如何使用相同的会话ID进入第二个页面,系统如何检索登录其他页面的用户详细信息 这是我的login.java代码

import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.HttpsTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class Login extends ActionBarActivity   {

    private static final String NAMESPACE = "***"; //WHAT IS A NAMESPACE
    private static final String URL = "***"; //removed HTTPS because I'm making a https call below
    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 
    protected void onCreate(Bundle savedInstanceState) { // WHAT DOES PROTECTED VOID MEAN? CAN YOU RENAME ANYTHING
        //SUCH AS SAVEDINSTANCESTATE OR IS IT ALWAYS LIKE THAT? 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login); //CAN YOU HAVE TWO ACTIVITIES TO ONE LAYOUT?
       usersusername = (EditText)findViewById(R.id.editusername);
       userspassword = (EditText)findViewById(R.id.editusername);
       LB = (Button) findViewById(R.id.loginbutton);
       LB.setOnClickListener(new View.OnClickListener() {

      public void onClick(View arg0) {
      loginAction();

      }
     });




    }


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

          //Pass value for userName variable of the web service
            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(unameProp);//Pass properties to the variable

          //Pass value for Password variable of the web service
            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);
           HttpTransportSE aht = new HttpTransportSE(URL);

           try {

                //URL = www.servicedeskattach.datacom.co.nz/axis/services/USD_R11_WebService?wsdl


                aht.call(SOAP_ACTION, envelope);  //this is the actual part that calls the web service
                String requestDumpString = aht.requestDump;
                String responseDumpString = aht.responseDump;




          //Get the soapresult from the envelope body
         //     SoapObject result = (SoapObject)envelope.bodyIn;
                SoapPrimitive response =(SoapPrimitive)envelope.getResponse();
                String status = response.toString(); // Result string
                TextView result = (TextView) findViewById(R.id.tv_status);
                   result.setText(response.toString());

                   if(status.equals("Success!"))
                    {
                       Intent intent = new Intent(Login.this,Dashboard.class);
                       intent.putExtra("username",usersusername.getText().toString());
                       startActivity(intent);


                    }
                   else
                    {
                       Intent i = new Intent(getApplicationContext(), Login.class);
                       startActivity(i);
                    }



           }
           catch (Exception e){

           }






        } 


    } 

这是跨活动持久化数据的一个很好的例子。为此,最常见的示例是使用“首选项”文件。如果您不希望数据持续很长时间,您可以在服务器端控制它(使cookie或会话ID过期):

使用意图传递数据并不坏,但如果打电话进来,应用程序移到后台,Android可能会杀死并重新启动你的应用程序,导致数据丢失。这听起来可能“安全”,但情况也可能是电话铃响或用户回复短信,如果这种情况经常发生,您的用户可能会感到恼火

数据库通常是多余的,除非您已经有一个数据库和一个用户对象等