Java 使用cookies保留会话Id
嗨,我有一个android应用程序,可以通过http请求连接到web服务。应用程序以登录请求开始。我想将会话Id保存在cookie中,以便在不同的活动之间使用相同的会话。所以我在登录请求中使用了cookie存储,然后在另一个请求中使用它。它似乎工作得很好,但我发现,一旦登录后进入另一个活动,在第二次尝试输入活动时,http请求不再工作,它返回空Json。我做错了什么?如果你能帮助我,我将不胜感激。 我在http类中的代码:Java 使用cookies保留会话Id,java,android,session,cookies,Java,Android,Session,Cookies,嗨,我有一个android应用程序,可以通过http请求连接到web服务。应用程序以登录请求开始。我想将会话Id保存在cookie中,以便在不同的活动之间使用相同的会话。所以我在登录请求中使用了cookie存储,然后在另一个请求中使用它。它似乎工作得很好,但我发现,一旦登录后进入另一个活动,在第二次尝试输入活动时,http请求不再工作,它返回空Json。我做错了什么?如果你能帮助我,我将不胜感激。 我在http类中的代码: import java.io.IOException; import j
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) {
}
调用
get()
会阻止ui线程等待结果。只需使用JsonLoader.execute(urlStr)
。非常感谢!那真的很有帮助!
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(); }
}