我的 Android 应用程序和 php 文件有问题。
我有两个 php 文件: * 第一个是在长 if/else block 之后连接:
[...]
else{
$response["message"]="Connexion OK";
session_start();
$_SESSION['id'] = $id;
$_SESSION['pseudo'] = $pseudo;
$_SESSION['options'] = $options;
$response['session'] = $_SESSION['id'];
}
[...]
echo json_encode($response);
我的第二个文件只在连接与否时返回:
<?php
session_start();
$response =0;
if(isset($_SESSION['id'])){
$response= 1;
}
echo $response;
?>
当我在 Firefox 中执行文件的 URL 时,顺序是: 1/第二个,我有 0 2/第一个 3/又是第二个,我得到 1
Php 文件构建良好!
现在,我想在 AsyncTasks Android 中执行此操作:无论我在哪个 Activity 中,我首先要测试用户与第二个文件的连接:但返回始终为 0...未连接,甚至在我使用第一个在 Connexion Activity 中调用的记录后。
Android 中还有什么可以打开 Php session 的吗?
感谢您的回答,
这是我的代码: 我有一个用于连接的 AsyncTask:
private class AddDataAsyncTask extends AsyncTask<Void, Void, Void> {
String message = "";
[...] //preExecute
@Override
protected Void doInBackground(Void... params) {
Log.i("add", " start doInBackground");
// Making a request to url and getting response
String urlt = url;
if (nameValuePair != null) {
String paramString = URLEncodedUtils.format(nameValuePair, "utf-8");
}
String response = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet post = new HttpGet(urlt);
HttpResponse rp = httpClient.execute(post);
if (rp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
// Server is unavailable
}
response = EntityUtils.toString(rp.getEntity()); //here is your response
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/* } catch (UnsupportedEncodingException e) {
e.printStackTrace();
*/
if (response != null) {
try {
JSONObject JsonResponse = new JSONObject(response);
message = JsonResponse.getString("message");
if (message.equals("Connexion OK"))
session = Integer.parseInt(JsonResponse.getString("session"));
Log.i("connnnnnexionC", response);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
[...]//后执行
还有我要检查连接的 AsyncTask:
private class VerifierConnexionAsyncTask extends AsyncTask<Void, Void, Void> {
[...] //preExecute
@Override
protected Void doInBackground(Void... params) {
String response = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Log.i("connnnnexion",response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
[...]//postExecute
}
首先我输入检查结果是:
我/connnnnexion:0
我转到 Connexion Activity 并使用第一个 AsyncTask 登录:我得到:
I/connnnnnexionC:{"message":"Connexion OK","session":"27"}
所以连接的很好
我重新输入支票,我又一次:
我/connnnnexion:0
解决方案 我写了一个新的 Class Connecte:
import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
public class Connecte {
static CookieStore cookieStore;
static HttpContext localContext;
public static void makeCookie(){
cookieStore = new BasicCookieStore();
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
}
在连接 AsyncTask 中我添加:Connexion.makeCookie():
HttpGet post = new HttpGet(urlt);
Connecte.makeCookie();
HttpResponse rp = httpClient.execute(post, Connecte.localContext);
在检查异步任务中我刚刚添加:
HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost, Connecte.localContext);
最佳答案
您必须确保您正在使用的连接正在重复使用相同的 cookie。 PHP 通过给每个客户端一个 PHP_SESSION cookie 来管理 session ,并根据您的 PHP_SESSION 跟踪各种值。如果您不在每个请求之间跟踪它,那么 php 会认为您每次都是不同的客户端。
CookieHandler.setDefault( new CookieManager( null, CookiePolicy.ACCEPT_ALL ) );
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost, localContext);
httpEntity = httpResponse.getEntity();
确保在所有 future 请求中重用相同的 localContext 对象,以便它们都共享相同的 cookie 并可以保持 PHP session 。
关于PHP session 在 Android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728983/