如何在android中的第一个活动中获得sessionid时维护活动之间的会话?

问题描述:

我想实现一个使用RESTful的web服务的应用程序。如何在android中的第一个活动中获得sessionid时维护活动之间的会话?

我在我的第一个活动中获得了我的会话ID。现在我需要在我导航到其他活动时保持会话。现在我正在收到认证证书是NULL。

任何人都可以帮助我吗?

我将第一个活动的sessionid传递给第二个活动。

我的第二个活动:

public class SetHome extends Activity { 

String applicationId,sessionId; 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home); 
    System.out.println("Entered thissss"); 

    applicationId = getIntent().getExtras().getString("applicationId"); 
    System.out.println("Application id is--->" + applicationId); 

       sessionId = getIntent().getExtras().getString("sessionId"); 
    System.out.println("SessionId is--->" + sessionId); 


    HttpConnectionRequest conn = new HttpConnectionRequest(); 
    conn.execute(); 


} 

private class HttpConnectionRequest extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     String url = "url/application/" 
       + applicationId; 

     try { 
      WebServiceClient client = new WebServiceClient(); 
      HashMap<String, String> hm = new HashMap<String, String>(); 
      hm.put("CONTENT-TYPE", "application/xml"); 
      hm.put("Accept", "application/xml"); 
      String sessionId; 

      String applicationId; 

      WebServiceResponse res = client.doGet(new URL(url), hm); 
      int resCode = res.getResponseCode(); 

      String response = res.getResponseContent(); 

      System.out.println(res); 
          */ 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

} 

public static String getValue(String xpath_expr, String xml) { 
    return BaseUtil.xpath_result(xpath_expr, xml); 
} 

public void relay(View v) { 

} 

public void tasks(View v) { 

} 

public void applications(View v) { 

    HttpConnectionRequest conn = new HttpConnectionRequest(); 
    conn.execute(); 

} 
} 

现在我越来越收到认证挑战为空。

+0

请解释在您的应用程序的其他活动中需要完成该会话ID。 – 2012-02-14 18:58:00

+0

如何维护会话?我无法在第二项活动中从Web服务中检索数据。我收到验证身份验证是空​​的。例外 – DroidGirl 2012-02-14 19:10:28

你可以用它们的许多ways.two是:

  1. 你可以使用Intent.putExtras(或者你可以使用捆绑,并把那个包里面意图)。
  2. 您可以使用sharedPreferences将数据永久放入第一个活动,并且您可以在第二个活动中访问它。
  3. 第一点的例子是here
  4. 观点二的例子是here

我会建议你创建延伸应用的一类。在那里制作公共变量,然后你可以从任何Activity获得MyApplicationClass app = (MyApplicationClass)getApplication(); app.myVariable;这个变量。您还需要将该类名放入清单中的<application>标记中。 More info