2014-07-06 42 views
1

我做這個Android應用程序,你的用戶必須登錄進入應用程序連接的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){ 

      } 






     } 


    } 

回答

0

這是其他網頁 登錄用戶的詳細信息一個堅持活動數據的好例子。要做到這一點,最常見的例子就是使用「首選項」文件。如果你不希望很長的數據要堅持你要麼控制它的服務器端(過期cookie或會話ID):

How do I get the SharedPreferences from a PreferenceActivity in Android?

使用意圖並不差來傳送數據,但如果手機電話進來,應用程序移動到後臺,Android可能會終止並重啓您的應用程序,導致數據丟失。這可能聽起來「安全」,但情況也可能是電話響鈴或用戶對短信作出響應,如果這種情況經常發生,您的用戶可能會感到惱怒。

數據庫通常是過度殺毒的,除非您已經有一個數據庫以及USER對象等。

相關問題