我正在构建一个使用 Web 服务(只是 php 脚本)来查询 mysql 数据库的 android 应用程序。目前没有任何身份验证系统,但我想在用户使用用户名和密码登录 mysqldatabase 后创建 php 会话......问题是目前我与脚本的所有连接都像这样工作:
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 会话的问题吗?或者服务器可以识别客户端吗?