使用cookie保持会话ID

时间:2014-06-04 09:16:23

标签: java android session cookies

您好我有一个Android应用程序连接到具有http请求的Web服务。应用程序以登录请求开始。我想将会话ID保存在cookie中,这样我就可以在不同的活动之间使用相同的会话。所以我使用cookie存储区作为登录请求,然后将其用于其他请求。它似乎工作正常,但我发现,一旦我在登录后进入另一个活动,第二次我尝试输入活动http请求不再工作,它返回一个空Json。我究竟做错了什么?如果你能帮助我,我将不胜感激。 我的代码在http类中:

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class HandleHttpConnection extends AsyncTask<String, Void, String> {

private ProgressDialog dialog;
private Context curr_context;
private CookieStore mCookie = null;
static private HttpContext localContext = null;

public HandleHttpConnection(Context context) {
    curr_context = context;
}

protected void onPreExecute() {

}

@Override
protected String doInBackground(String... params) {
    Log.i("msg", "Its working");
    String jsonInStringFormat = null;
    DefaultHttpClient httpclient = new DefaultHttpClient();

    try {
        HttpResponse httpResponse = null;

        //if we are logging in we want to remember the session ID 
        if (params[0].contains("login")) {
            Log.i("msg", "its on login");

            System.out.println("executing request login" + params[0]);
            // Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();

            // Create local HTTP context
            localContext = new BasicHttpContext();

            // Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            // Pass local context as a parameter
            httpResponse = httpclient.execute(new HttpGet(params[0]), localContext);
            HttpEntity entity = httpResponse.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(httpResponse.getStatusLine());

            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }

            List<Cookie> cookies = cookieStore.getCookies();
            System.out.println("cookie first: " + cookies.get(1));
        }
        else {
            Log.i("msg", "its not login");
            System.out.println("executing request" + params[0]);
            httpResponse = httpclient.execute(new HttpGet(params[0]),localContext);
        }

        // this is the JSON content in String format. You need to convert this to actual JSON
        jsonInStringFormat = EntityUtils.toString(httpResponse.getEntity());

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        httpclient.getConnectionManager().shutdown();
    }

    return jsonInStringFormat;
}

protected void onPostExecute(String result) {
}

}

这是我使用http reqeust的活动之一:

private void setViewsProgramaticly() {
TextView name = (TextView)findViewById(R.id.nameProfileText);
TextView email = (TextView)findViewById(R.id.emailProfileText);
TextView membership = (TextView)findViewById(R.id.memberSinceText);
String urlStr = "http://IpAddress:8000/user/view";
HandleHttpConnection JsonLoader = new HandleHttpConnection(context);
try {
    String jsonStr = JsonLoader.execute(urlStr).get();
    System.out.println(jsonStr);
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace(); }
}

0 个答案:

没有答案