2012-07-23 25 views
13

tôi đã phát triển thành công một biểu mẫu đăng nhập. Nhưng bây giờ tôi không thể quản lý phiên làm việc ... tôi cũng đã đọc nhiều khái niệm phiên hơn. Nhưng tôi không thể làm điều đó. Vui lòng giải thích chi tiết cho tôi. Đây là mã của tôi. Vui lòng giúp tôi làm cách nào để sử dụng Phần phiên tại đây.Tôi làm cách nào để quản lý đăng nhập/đăng xuất Android?

Đây là một phần mã của tôi:

package com.androidlogin.ws; 

import org.ksoap2.SoapEnvelope; 
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 android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AndroidLoginExampleActivity extends Activity { 
private final String NAMESPACE = "http://ws.userlogin.com"; 
private final String URL = "http://111.223.128.10:8085/AndroidLogin/services/Login?wsdl"; 
private final String SOAP_ACTION = "http://ws.userlogin.com/authentication"; 
private final String METHOD_NAME = "authentication"; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button login = (Button) findViewById(R.id.btn_login); 
    login.setOnClickListener(new View.OnClickListener() { 

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

    } 
    }); 
    } 

private void loginAction(){ 
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    EditText userName = (EditText) findViewById(R.id.tf_userName); 
    String user_Name = userName.getText().toString(); 
    EditText userPassword = (EditText) findViewById(R.id.tf_password); 
    String user_Password = userPassword.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); 
    envelope.setOutputSoapObject(request); 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    try{ 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 

      TextView result = (TextView) findViewById(R.id.tv_status); 
      result.setText(response.toString()); 

    } 
    catch(Exception e){ 

    } 
    } 

    } 
+0

Dịch vụ web bạn tiêu thụ, Bạn có quyền truy cập vào cơ sở mã của nó? hoặc bạn phải sử dụng nó theo cách của họ? – Waqas

+0

kiểm tra liên kết này http://stackoverflow.com/questions/4224913/android-session-management –

+0

liên kết ở trên không hữu ích cho tôi. Vui lòng giải thích cho tôi cách thêm phần mã phiên là phần mã hóa của tôi. –

Trả lời

2

séc này .. nó sẽ giúp bạn duy trì các phiên trong Android

https://stackoverflow.com/a/6112799/1358004

Android App Strategy for keeping track of a login session (liên kết này sẽ hiển thị tất cả các khía cạnh trong -các điều khoản duy trì các phiên đăng nhập/đăng xuất.

Tôi đoán nó sẽ giúp y ou.

+0

nó rất hữu ích cho giữ tên người dùng cho đến khi đăng xuất .. nhưng sau khi đăng xuất nút có nghĩa là nó được đi vào hoạt động đăng nhập. Tôi sẽ trở lại hoạt động của tôi trên giả lập của tôi có nghĩa là nó được hiển thị tên người dùng hiển thị activity.but tôi muốn cần nếu tôi ll quay trở lại có nghĩa là nó là chuyển hướng đến hoạt động đăng nhập .. như vậy ở đây sẽ quản lý các khái niệm phiên .. vì vậy hãy giải thích các khái niệm quản lý phiên sau khi nhấp vào nút đăng xuất cũng có. –

12

Xóa chồng hoạt động khi thực hiện thao tác đăng xuất.

Intent intent = new Intent(this, LoginActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 

Sau đó nút quay lại sẽ ở tại LoginActivity do ngăn xếp hoạt động bị xóa. Tôi đoán nó sẽ giúp bạn.

1

Bạn có thể xóa phiên của bạn như là

MySession session = MySession .getInstance(MainActivity.this); 
        session.clear(); 
        Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
        finish(); 
1

chỉ cần gọi finish() một lần vào nút logout được nhấp cách để sử dụng:

trong cuộc gọi hoạt động ==>finish();

trong cuộc gọi fragment = =>context.finish();

hy vọng sẽ hữu ích.

+0

giải pháp này làm việc cho tôi cảm ơn rất nhiều .. – EaBangalore

Các vấn đề liên quan