0
我正在构建一个使用Web服务(只是php脚本)来查询mysqldatabase的android应用程序。目前没有任何身份验证系统,但我想在用户使用mysqldatabase上的用户名和密码登录后创建php会话......问题是,目前我所有的脚本连接都是这样工作的:用web服务上的php会话识别android httpclient
public static long getOnlineAlarm(long taskID) {
long alarm = -1;
String result = null;
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("taskID", String.valueOf(taskID)));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://192.168.1.13/spotnshare/getOnlineAlarm.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.i("taghttppost", "" + e.toString());
}
// conversion de la réponse en chaine de caractère
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.i("tagconvertstr", "" + e.toString());
}
// recuperation des donnees json
try {
Log.i("tagconvertstr", "[" + result + "]");
JSONObject jObj = new JSONObject(result);
alarm = jObj.getLong("alarm");
return alarm;
} catch (JSONException e) {
Log.i("tagjsonexp", "" + e.toString());
} catch (ParseException e) {
Log.i("tagjsonpars", "" + e.toString());
}
return alarm;
}
所以我创建了一个新的DefaultHttpClient();每次我使用脚本时,这会对php会话造成问题吗?或者服务器是否可以识别客户端?
是的,但我们如何保持会话ID在服务器端而不访问数据库?用cookies? – Karly 2012-08-02 19:42:37
要么使用会话,要么有一个跟踪会话的数据库表 - 基本上保留sessionID并具有到期日期/时间,以便会话ID在一段时间后不再有效。 例如一个包含sessionID,userID,超时的表。 如果您需要额外的安全性,您可能还需要包含一个IP地址或类似的识别值,以防止sessionID从其他位置传递。 – hyarion 2012-08-03 14:03:31