我的Android应用和我的php文件存在问题。
我有两个php文件: *第一个是在长if / else块之后连接:
[...]
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;
&GT;
当我在Firefox中执行文件的URL时,按此顺序: 1 /第二个,我有0 2 /第一个 3 /第二个,我得到1
Php文件很好构建!
现在,我想在AsyncTasks Android中执行此操作:无论我在哪里进行活动,我首先想要使用第二个文件测试用户连接:但返回始终为0 ...未连接,甚至在我使用在Connexion活动中调用的第一个登录后。
Android还有什么东西可以打开Php会话吗?
感谢您的回答,
这是我的代码: 我有一个用于连接的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;
[...] // postExecute }
我的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
}
首先我输入检查结果是:
I / connnnnexion:0
我转到Connexion活动,我使用第一个AsyncTask登录:我得到了:
I / connnnnnexionC:{“message”:“Connexion OK”,“session”:“27”}
所以它很好地连接
我重新进入支票,我又一次:
I / 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);
在check asynctask中我只想添加:
HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost, Connecte.localContext);
答案 0 :(得分:2)
您必须确保您使用的连接正在重复使用相同的Cookie。 PHP通过为每个客户端提供PHP_SESSION cookie来管理会话,并根据您的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();
确保在将来的所有请求中重用相同的localContext对象,以便它们共享相同的cookie并且可以持久保存PHP会话。